Python:如何使用用户输入关闭或继续while循环

Python:如何使用用户输入关闭或继续while循环,python,loops,if-statement,input,while-loop,Python,Loops,If Statement,Input,While Loop,我是python新手,仍在尝试了解基本知识。我在网上寻找答案,但似乎没有一个解决方案适合我。我不知道我是否遗漏了一些小的东西,或者我的总体结构是错误的。我的代码的计算部分的工作方式与预期的一样 我的问题是,我不知道如何要求用户输入是或否,从而导致: (答案为“是”,重新启动循环,以便用户可以尝试其他计算) (回答“否”关闭循环/结束程序) 请告诉我你的建议 play = True while play: hours = float(input("Please enter th

我是python新手,仍在尝试了解基本知识。我在网上寻找答案,但似乎没有一个解决方案适合我。我不知道我是否遗漏了一些小的东西,或者我的总体结构是错误的。我的代码的计算部分的工作方式与预期的一样

我的问题是,我不知道如何要求用户输入是或否,从而导致:

(答案为“是”,重新启动循环,以便用户可以尝试其他计算)

(回答“否”关闭循环/结束程序)

请告诉我你的建议

    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
play=True
游戏时:
小时数=浮动(输入(“请输入本周的工作小时数:”)
费率=浮动(输入(“请输入您的小时工资:”)
#如果少于40小时
如果40小时<54小时:
timeandhalf=费率*1.5
工资=(40小时*费率)+(40小时)*分半时)
打印“您本周的工资是”,支付
#双倍时间超过54小时
elif小时数>54:
timeandhalf=费率*1.5
加倍时间=速率*2
工资=(40小时*费率)+(40小时)*半小时)+(54小时)*双倍时间)
打印“您本周的工资是”,支付
答案=(输入(“您想再试一次吗?:”)
如果str(回答)=“是”:
播放=正确
其他:
播放=错误

我认为您应该让用户输入是或否,直到他这样做

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'
是/否的变体可以在和Vicki Laidler的答案中找到。

您正在使用Python2.x尝试将输入作为有效的Python表达式运行。输入字符串时,它会尝试在命名空间中查找该字符串,如果找不到,则会抛出错误:

名称错误:未定义名称“是”

您不应该使用
input
来接收未经过滤的用户输入,这可能很危险。相反,请使用返回字符串的:

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()
play=True
游戏时:
小时数=浮动(原始输入(“请输入本周工作小时数:”)
费率=浮动(原始输入(“请输入您的小时工资:”)
#如果少于40小时
如果40小时<54小时:
timeandhalf=费率*1.5
工资=(40小时*费率)+(40小时)*分半时)
打印“您本周的工资是”,支付
#双倍时间超过54小时
elif小时数>54:
timeandhalf=费率*1.5
加倍时间=速率*2
工资=(40小时*费率)+(40小时)*半小时)+(54小时)*双倍时间)
打印“您本周的工资是”,支付
答案=原始输入(“您想再试一次吗?:”)。下限()
尽管如此:
如果答案=‘是’:
播放=正确
打破
elif答案==‘否’:
播放=错误
打破
其他:
回答=原始输入('选项不正确。键入“是”重试,或键入“否”退出):')。下限()

可能重复@idjaw我有点同意你的看法,但OP似乎并不关心输入是否有效。他们认为输入将是有效的。@ChristianDean当然知道-有效输入是“否”,否则继续!我不知道如何让“是”和“否”实际执行任何操作。现在我知道了!
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()