Python时代猜谜游戏

时间:2015-02-11 22:13:26

标签: python-2.7

我正在尝试制作一款能猜出你年龄的游戏。我得到它的工作,但我只能以1的增量上升。我想让它以更高的增量开始并降低。如果我添加一个额外的if语句,我的游戏会中断。

x = 25

print"I am going to guess your age"

print("Are you " +\
      str(x) + \
      ", or are you older or younger? " )

guess = raw_input()

while guess != "yes":

      if guess == "older":
            x = x + 1
            print(x)
            guess = raw_input("Are you " +\
                              str(x) +\
                              ", or are you older or younger? " + "\n")
      elif guess == "younger":
            x = x - 1
            print(x)
            guess = raw_input("Are you " +\
                              str(x) +\
                              ", or are you older or younger? " + str(x) + "\n")
print"I got it, you are " + str(x)

1 个答案:

答案 0 :(得分:0)

如何添加第二个计数器...这不是很好,只是为了让你开始....

x = 25
y = 20
print"I am going to guess your age"

print("Are you " +\
  str(x) + \
  ", or are you older or younger? " )

guess = raw_input()

while guess != "yes":

  if guess == "older":
        x = x + y
        y = y/2
        print(x)
        guess = raw_input("Are you " +\
                          str(x) +\
                          ", or are you older or younger? " + "\n")
  elif guess == "younger":
        x = x - y
        y = y/2
        print(x)
        guess = raw_input("Are you " +\
                          str(x) +\
                          ", or are you older or younger? " + str(x) + "\n")
print"I got it, you are " + str(x)

编辑,将y从5更改为20,并将y = y - 1更改为y = y / 2现在它的工作方式与预期相符。

好一点,但这个将重新使用相同的数字

x = 25
y = 10
print"I am going to guess your age"

print("Are you " +\
  str(x) + \
  ", or are you older or younger? " )

guess = raw_input()
previousGuess = ""
while guess != "yes":

  if guess == "older":
        x = x + y
        if guess != previousGuess :
            y = y/2
        print(x)
        previousGuess = guess
        guess = raw_input("Are you " +\
                          str(x) +\
                          ", or are you older or younger? " + "\n")
  elif guess == "younger":
        x = x - y
        if guess != previousGuess :
            y = y/2
        print(x)
        previousGuess = guess
        guess = raw_input("Are you " +\
                          str(x) +\
                          ", or are you older or younger? " + str(x) + "\n")
print"I got it, you are " + str(x)