你好,我知道這已問了幾次,但我無法找到答案。 問題是關於我的數字遊戲的反面猜測。 代碼執行程序,但不是以「人性化」的方式執行。如果數字是50,它猜到20,它的響應更高,例如計算機說30,它得到的答案更低,它猜測15。我該如何解決?練習來自:絕對初學者的Python。 有人可以幫我嗎?請以簡單的方式,否則我會跳過書中的內容。 我想你可以通過查看代碼來了解我所知道的以及不知道的內容。 請幫我...反向遊戲python
代碼:
#Guess My Number
#
#The computer picks a random number between 1 and 100
#The playes tries to guess it and the coputer lets
#the player know if the guess is too high, too low
#or right on the money
print ("\t// // // // // // // // // //")
print ("\tWelcome to 'Guess My Number'!")
print ("\tComputer VS Human")
print ("\t// // // // // // // // // //")
name = input("What's your name?")
print ("Hello,", name)
print ("\nOkay, think of a number between 1 and 100.")
print ("I'll try to guess it within 10 attemps.")
import random
#set the initial values
the_number = int(input("Please type in the number to guess:"))
tries = 0
max_tries = 10
guess = random.randint(1, 100)
#guessing loop
while guess != the_number and tries < max_tries:
print("Is it", guess,"?")
tries += 1
if guess > the_number and tries < max_tries:
print ("It's lower")
guess = random.randint(1, guess)
elif guess < the_number and tries < max_tries:
print ("It's higher")
guess = random.randint(guess, 100)
elif guess == the_number and tries < max_tries:
print("Woohoo, you guessed it!")
break
else:
print("HAHA you silly computer it was", the_number,"!")
input ("\n\nTo exit, press enter key.")
謝謝,但是我在代碼中的位置呢? – bogaardesquat
在'import random'後面聲明兩個變量'lowest'和'highest'。在if語句的「lower」和「higher」中,重新調整變量的值。任何時候你猜你會用'guess = random.randint(最低,最高)' – Lanaru