在Python3中工作。在
我对Python还比较陌生(只有几个星期的知识)。在
这个程序给我的提示是写一个随机数游戏,用户必须猜出随机数(1到100之间),如果不正确,就会提示它太低或太高。然后用户会反复猜测,直到找到解决方案。在解出后,猜测的次数应该在最后吻合。在import random
def main():
# initialization
high = 0
low = 0
win = 0
number = random.randint(1, 100)
# input
userNum = int(input("Please guess a number between 1 and 100: "))
# if/else check
if userNum > number:
message = "Too high, try again."
high += 1
elif userNum == number:
message = "You got it correct! Congratulations!"
win += 1
else:
message = "Too low, try again."
low += 1
print()
print(message)
# loop
# while message != "You got it correct! Congratulations!":
# display total
print()
print("Number of times too high: ", high)
print("Number of times too low: ", low)
print("Total number of guesses: ", (high + low + win))
main()
我在努力想办法让这个循环工作。我需要随机数是静态的,而用户猜测与输入。每次尝试之后,我还需要提示他们if/else检查中的正确消息。在