我正在尝试制作一个我认为是数字的游戏,计算机必须猜测它。这就是我所拥有的:
import random
low = 1
high = 100
def guess_attempt():
guess_attempt = random.randint(low, high)
return guess_attempt
def game():
print("Lets play a game, think in number and I will try to guess it")
print("Is the number ", guess_attempt()," that you thought?")
response = eval(input("Type L if its to low, H if its to high or C if its correct "))
if response == "L":
low = guess_attempt
if response == "H":
high = guess_attempt
if response == "C":
high = 100
low = 1
while(response != 'C'):
print("Okay, lets try again")
print("I knew I could guess it")
game()
当我让计算机知道它的号码错误时,我不知道怎么让它重新启动
答案 0 :(得分:0)
执行此操作的标准方法是将代码的一部分提出问题并在无限循环中接收答案,如果您提供正确的答案(或退出命令),程序将忽略该循环。
答案 1 :(得分:0)
我做了一些改动。以下代码有效,
import random
def guess_attempt(low, high):
return random.randint(low, high)
def game():
low = 1
high = 100
print("Lets play a game, think in number between 1 and 100. I will try to guess it")
g = guess_attempt(low, high)
print("Is the number {} that you thought?".format(g))
response = input("Type L if its to low, H if its to high or C if its correct ").strip()
while(response != 'C'):
if response == 'L':
low = g
elif response == 'H':
high = g
g = guess_attempt(low, high)
print("Is the number ",g," that you thought?")
response = input("Type L if its to low, H if its to high or C if its correct ").strip()
print("I knew I could guess it is {}".format(g))
game()