3

I am new to python, and still trying to figure out the basics. I've looked for an answer online, but no solutions seem to work for me. I don't know if I'm missing something small, or my total structure is wrong. The calculation portion of my code works like it's supposed to.

My problem is, I can't figure out how to ask the user to input yes or no resulting in:

(the answer YES restarting the loop so the user can try another calculation)

(the answer NO closing the loop/ending the program)

Please let me know what you suggest!

    play = True

while play:

    hours = float(input("Please enter the number of hours worked this week: "))
    rate = float(input("Please enter your hourly rate of pay: "))

    #if less than 40 hrs
    if hours <= 40:
        pay = hours * rate
        print "Your pay for this week is",pay

    #overtime 54hrs or less   
    elif hours > 40 < 54:
        timeandhalf = rate * 1.5
        pay = (40 * hours * rate) + ((hours - 40) * timeandhalf)
        print "Your pay for this week is",pay 

    #doubletime more than 54hrs        
    elif hours > 54:
        timeandhalf = rate * 1.5
        doubletime = rate * 2
        pay = (40 * hours * rate) + ((hours - 40) * timeandhalf) + ((hours - 54) * doubletime)
        print "Your pay for this week is",pay    


    answer = (input("Would you like to try again?: "))
    if str(answer) == 'yes':
        play = True
    else:
        play = False
Savannah Nicole
  • 45
  • 1
  • 1
  • 5
  • 2
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – idjaw Jun 26 '17 at 00:15
  • @idjaw I kinda agree with you, but the OP doesn't seem to care about whether the input is valid or not. They assume the input _will_ be valid. – Christian Dean Jun 26 '17 at 00:17
  • @ChristianDean of course they do - valid input is "no" otherwise continue – Uriel Jun 26 '17 at 00:25

3 Answers3

1

You are use Python 2.x. input() tries to run the input as a valid Python expression. When you enter a string, it tries to look for it in the namespace, if it is not found it throws an error:

NameError: name 'yes' is not defined

You should not use input to receive unfiltered user input, it can be dangerous. Instead, use raw_input() that returns a string:

play = True

while play:

    hours = float(raw_input("Please enter the number of hours worked this week: "))
    rate = float(raw_input("Please enter your hourly rate of pay: "))

    #if less than 40 hrs
    if hours <= 40:
        pay = hours * rate
        print "Your pay for this week is",pay

    #overtime 54hrs or less   
    elif hours > 40 < 54:
        timeandhalf = rate * 1.5
        pay = (40 * hours * rate) + ((hours - 40) * timeandhalf)
        print "Your pay for this week is",pay 

    #doubletime more than 54hrs        
    elif hours > 54:
        timeandhalf = rate * 1.5
        doubletime = rate * 2
        pay = (40 * hours * rate) + ((hours - 40) * timeandhalf) + ((hours - 54) * doubletime)
        print "Your pay for this week is",pay    

    answer = raw_input("Would you like to try again?: ").lower()
    while True:
        if answer == 'yes':
            play = True
            break
        elif answer == 'no':
            play = False
            break
        else:
            answer = raw_input('Incorrect option. Type "YES" to try again or "NO" to leave": ').lower()
FJSevilla
  • 3,733
  • 1
  • 13
  • 20
0

I think you should ask user to input yes or no untill he does it.

while play:
    # Here goes your code

    while True:
        answer = input("Would you like to try again? Yes or No?").lower()
        if answer in ('yes', 'no'):
            break

    play = answer == 'yes'
Igor Yudin
  • 393
  • 4
  • 10
-1
var = raw_input("Would you like to try again: ")

print "you entered", var

Variations for yes/no can be found in this question and Vicki Laidler's answer for it.

Juan T
  • 1,219
  • 1
  • 10
  • 21
Jason Enochs
  • 1,436
  • 1
  • 13
  • 20