2

我的代码几乎是这样的:

Rate = input ("Enter Desired Rate of Charge: ") #User will be prompted to enter the charge rate of the system
if Rate < 0.5:
#If the charge rate entered is less than 0.5 kWhs
    print "Charge Rate Too Low: Please consider revision" #Tells the user the rate is too low
elif Rate > 2.0:
#Also, if the charge rate entered is greater than 2.0 kWhs...
    print "Charge Rate Too High: Please consider revision" #Tells the user the rate is too high
else:
#Otherwise, for any number entered between the bounds of 0.5 and 2.0 kWhs...
    print '\n' #Page break for new conditions.

如果输入的整数小于 0.5 或大于 2,我需要它重新提示用户,当用户这样做时,将该整数保存为 Rate 并继续。谢谢你。

4

2 回答 2

2

我认为这比avasal的答案要干净一些。此解决方案假定用户知道如何使用 CTRL+C 退出应用程序,否则您应该添加对在用户输入 Q 或其他内容时退出程序的支持。

rate = 0

while True:
    rate = input("Enter desired rate of charge: ")

    if not 0.5 < rate < 2:
        print "Rate must be between 0.5 and 2."
    else:
        break
于 2012-12-06T05:24:25.477 回答
0

添加一个while循环直到速率小于 0.5 或速率大于 2.0

Rate = 0
while (Rate < 0.5) or (Rate > 2.0):
    Rate = input ("Enter Desired Rate of Charge: ") #User will be prompted to enter the charge rate of the system
    if Rate < 0.5:
    #If the charge rate entered is less than 0.5 kWhs
        print "Charge Rate Too Low: Please consider revision" #Tells the user the rate is too low
    elif Rate > 2.0:
    #Also, if the charge rate entered is greater than 2.0 kWhs...
        print "Charge Rate Too High: Please consider revision" #Tells the user the rate is too high
    else:
    #Otherwise, for any number entered between the bounds of 0.5 and 2.0 kWhs...
        print '\n' #Page break for new conditions.
于 2012-12-06T05:18:42.290 回答