我是编码初学者,我正在学习 python 并决定制作一个随机数猜测游戏。一切都很顺利,但我想添加一个猜测计数器,我不确定为什么总数不会更新。
def random_num():
guess = int(input("Guess the number, it is between 1-100: "))
total = 0
if guess > random_number:
print("\nLower")
total += 1
random_num()
elif guess < random_number:
print("\nHigher")
total += 1
random_num()
elif guess == random_number:
print("\nYou win")
print("You took " + str(total) + " guesses to get the correct number.")
总输出为 0,但这是不正确的。
浮云间
不负相思意
相关分类