在列表中进行一些计算后,将结果变量存储起来

时间:2014-10-03 07:39:33

标签: python list python-2.7 random

我正在尝试为计算机创建一个反向数字游戏来猜测我的号码。该计划旨在以这种方式运行:

  • 我收到了一个号码
  • 如果我输入'>','<'响应给定的数字,计算机应将数字加到1或100并除以2,具体取决于它是低还是高。
  • 之后,计算机会询问结果号码是否为我的号码
  • 依此类推,直到我输入' ='。

我的问题是在计算将随机数添加到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

1 个答案:

答案 0 :(得分:0)

rand_number不会在代码中的任何位置发生变化。 在while循环中,脚本在同一个随机数上进行8次迭代。

因此,无论用户猜测,您总是会:

rand_number = random.randint(1, 100) 所有8次迭代的内部循环 low_list = (9 + 1) // 2