您好,我知道这已经被问了几次,但我找不到答案。问题是关于反向猜我的数字游戏。代码执行程序,但不是以“类人”的方式。如果数字是 50 并且它猜测 20 它响应更高,例如计算机说 30,它得到响应更低它猜测 15。我该如何解决这个问题?练习来自:绝对初学者的 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.")