我对python还不太熟悉,仍然在努力搞清楚基础知识。我在网上寻找答案,但似乎没有解决办法。我不知道我是否遗漏了一些小东西,或者我的整体结构有问题。我的代码的计算部分的工作方式和它应该的一样。
我的问题是,我不知道如何让用户输入yes或no,结果是:
(回答是重新启动循环以便用户可以尝试其他计算)
(答案是不关闭循环/结束程序)
请告诉我你的建议!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