嗯,我很新的编程,我需要一些帮助,这个代码。尝试代码中的功能
roundvalue = True
rounds = 0
while roundvalue == True:
rounds= input("Pick a number of rounds (3 or 5)")
try:
int(rounds)
except ValueError:
try:
float(rounds)
except ValueError:
print ("This is not a number")
if rounds == 3:
print("You chose three rounds!")
roundvalue = False
elif rounds == 5:
print ("You chose 5 rounds")
roundvalue = False
else:
print ("Input again!")
代码的点是选择了数发子弹,如果用户输入什么都应该重复的问题(即不是3或5个字母或数字)。 *(我的代码只是目前重复“挑选了数发子弹(3或5)”
这是你真正的缩进? –
这可能是对你有用:[要求用户输入,直到他们给出有效响应](http://stackoverflow.com/q/23294658/953482) – Kevin
缩进在Python中很重要。如图所示,while循环只围绕'rounds ='部分,它不包含'try ... except'。另外'int(rounds)'不会改变'rounds'的值,因此即使你为'rounds'输入'3','rounds == 3'总是'False',因为'rounds'仍然是一个字符串这点。另外:为什么“浮动(轮)”? – dhke