【发布时间】:2016-10-31 13:20:42
【问题描述】:
#Write a short program that will do the following
#Set a value your favorite number between 0 and 100
#Ask the user to guess your favorite number between 0 and 100
#Repeat until they guess that number and tell them how many tries it took
#If the value they guessed is not between 0 and 100
#tell the user invalid guess and do not count that as an attempt
我的问题是,即使用户猜到了 0 到 100 之间的数字,它仍然会打印出“猜错。再试一次”。如果输入可接受(1-100),我如何控制我的循环跳过打印语句和问题重复?提前致谢!
favoriteNumber = 7
attempts = 0
guess = raw_input("Guess a number between 0 and 100: ")
if (guess < 0) or (guess > 100):
print "Invalid guess. Try again"
guess = raw_input("Guess a number between 0 and 100: ")
attempts1 = str(attempts)
print "it took " + attempts1 + "attempts."
【问题讨论】:
-
什么循环?您的代码中没有任何循环。
标签: python validation loops input