循环终止后的存储值

时间:2015-02-18 00:34:30

标签: python while-loop raw-input

关于如何存储用户输入的最后一个值的建议,即当raw_input()返回空字符串('')时(按下Enter键)。对于例如如果用户值为50.如何将其存储在变量“dollar”

这是在循环终止后

while dollar < 55 or  num_rate > 55: 
    print dollar, "is outside the permitted range, try again"
    dollar = float(raw_input("Enter value between 55 and 65: ")) 

1 个答案:

答案 0 :(得分:1)

如果用户可以点击输入而不实际输入任何内容,您应该优雅地处理ValueError错误。

假设dollar已在while块之上的某处初始化:

while num_rate > 55.0:
    try:
        dollar = float(raw_input("Enter value between 55 and 65: "))
    except ValueError:
        break
    else:
        if 55.0 < dollar < 65.0:
            break

        print dollar, " is outside the permitted range, try again"

print "last value of dollar: ", dollar