Python:如何使用用户输入来关闭或继续while循环 [英] Python: How to use user input to close or continue a while loop
问题描述
我是python的新手,并且仍在尝试弄清楚基础知识.我一直在网上寻找答案,但似乎没有任何解决方案适合我.我不知道我是否缺少一些小东西,或者我的总体结构是错误的.我的代码的计算部分按预期工作.
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)
请让我知道您的建议!
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
推荐答案
您正在使用Python2.x. input()
尝试将输入作为有效的运行Python表达式.输入字符串时,它将尝试在名称空间中查找它,如果找不到,则会引发错误:
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:名称'yes'未定义
NameError: name 'yes' is not defined
您不应使用input
来接收未经过滤的用户输入,这很危险.而是使用 raw_input()
返回一个字符串:
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()
这篇关于Python:如何使用用户输入来关闭或继续while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!