关于如何存储用户输入的最后一个值的建议,即当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: "))
答案 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