我写了一个游戏,玩家试图猜测一个随机数(见下面的代码)。还提供了诸如“太低……”和“太高……”之类的建议。但是如果反过来让计算机猜测玩家选择的数字呢?我对此有困难,我不知道为什么。我认为我需要某人的“推动”,而不是实际的代码,因为我需要自己尝试。如果有人能帮助我,我将不胜感激。
这是玩家必须猜测数字的代码(Python 3):
#Guess my Number - Exercise 3
#Limited to 5 guesses
import random
attempts = 1
secret_number = random.randint(1,100)
isCorrect = False
guess = int(input("Take a guess: "))
while secret_number != guess and attempts < 6:
if guess < secret_number:
print("Higher...")
elif guess > secret_number:
print("Lower...")
guess = int(input("Take a guess: "))
attempts += 1
if attempts == 6:
print("\nSorry you reached the maximum number of tries")
print("The secret number was ",secret_number)
else:
print("\nYou guessed it! The number was " ,secret_number)
print("You guessed it in ", attempts,"attempts")
input("\n\n Press the enter key to exit")
谢谢你的帮助。