我正在尝试为计算机创建一个反向数字游戏来猜测我的号码。该计划旨在以这种方式运行:
我的问题是在计算将随机数添加到1或100并除以2之后,然后询问这是否是我的数字我试图存储该新数字,以便它不会重复,但它会不断重复。这是代码:
import random
guesses_taken = 0
my_name = raw_input("Hello! What is your name? ")
print ("Well {} think of a number between 1 and 100!").format(my_name)
low_list_store = list()
high_list_store = list()
rand_number = random.randint(1,100)
print("Is your number greater (>), equal (=), or less (<) than "
+ str(rand_number) + "?")
while guesses_taken < 8:
user_response = raw_input("Please answer <, =, >: ")
guesses_taken = guesses_taken + 1
if user_response == '<':
low_list = (rand_number + 1) // 2
print("Is your number greater (>), equal (=), or less (<) than {}?").format(low_list)
low_list_store.insert(0,low_list)
elif user_response == '>':
high_list = (rand_number + 100) // 2
print("Is your number greater (>), equal (=), or less (<) than {}?").format(high_list)
high_list_store.insert(0,high_list)
if user_response == '=':
print("I have guessed it!!")
guesses_taken = str(guesses_taken)
print("I needed {} step!!").format(guesses_taken)
break
if guesses_taken == 8:
print("Sorry i'm not good at this game..")
if user_response <> '>' and user_response <> '<' and user_response <> '=':
print("Please input a valid response!!")
continue
答案 0 :(得分:0)
rand_number不会在代码中的任何位置发生变化。 在while循环中,脚本在同一个随机数上进行8次迭代。
因此,无论用户猜测,您总是会:
rand_number = random.randint(1, 100)
所有8次迭代的内部循环
low_list = (9 + 1) // 2