我正在尝试创建一个程序,用于存储1到0之间的用户输入。
当我只输入数字时它工作正常,但是,当输入任何其他内容时程序会失败。
我尝试在while语句中添加第三个子句,但这使整个程序失败。
我怀疑float(input)
是错误的代码 - 当我没有这个时,程序中稍后的总和会失败,表明该变量只是一个字符串。
这里有什么问题?
# Juvenile survival rate must be between 0 and 1 #
juvenile_survival = 3
while juvenile_survival < 0 or juvenile_survival > 1 or juvenile_survival.isdigit() == False:
juvenile_survival = float(input("What is your juvenile survival rate? Remember this should be between 0 and 1"))
print("Your juvenile survival rate is ", juvenile_survival)
答案 0 :(得分:3)
我不确定你想要完成什么:
你想要这样的东西:
juvenile_survival = 3
while juvenile_survival < 0 or juvenile_survival > 1:
try:
juvenile_survival = int(input("What is your juvenile survival rate? Remember this should be between 0 and 1: "))
except ValueError as ex:
print(ex)
print("Your juvenile survival rate is ", juvenile_survival)