猜不同名字的数字游戏

Guess the Number Game with Different Guess Names

这是我第一次使用 Whosebug 访问——我是编程新手,正在参加 Python 的初学者课程。很高兴开始!

我们的第二个任务要求我们创建著名的猜数字游戏。对于那些已经知道这个游戏的人,我很乐意为它添加的额外部分提供一些帮助:我们必须列出每个猜测及其各自的顺序。示例输出应如下所示:

我已经把代码写到我猜 1 和猜 3 出现的地方,但我无法让猜 2 出现。我一直在修改和替换每个 "while"、"if"、"elif" 和 "else" 命令来解决这个问题,但似乎无法提出解决方案!到目前为止,这是我的代码:

def guess():
print ("I'm thinking of an integer, you have three guesses.")
attempts = 0
from random import randint
number = randint(0,10)
guess = eval(input("Guess 1: Please enter an integer between 1 and 10: "))
while guess != number and attempts == 0:
    if guess < number:
        print("Your guess is too small.")
        break
    if guess > number:
        print("Your guess is too big.")
        break
    elif guess == number:
        print("You got it!")
        attempts = attempts + 1
if number != guess and attempts == 1:
    guess = eval(input("Guess 2: Please enter an integer between 1 and 10: "))
    if guess < number:
        print("Your guess is too small.")
    elif guess > number:
        print("Your guess is too big.")
    while guess == number:
        print("You got it!")
        attempts = attempts + 1
elif number != guess and attempts == 2:
    guess = eval(input("Guess 3: Please enter an integer between 1 and 10: "))
    if guess < number:
        print("Too bad. The number is: ", number)
    elif guess > number:
        print("Too bad. The number is: ", number)
    while guess == number:
        print("You got it!")

此代码输出 Guess 1 然后退出。谁能帮我弄清楚如何让 Guess 2 和 3 出现?欢迎所有想法--谢谢!

您可以大大缩短您的代码,只需在循环中移动输入并继续循环使用范围尝试三次或用户猜对:

def guess():
    print ("I'm thinking of an integer, you have three guesses.")
    from random import randint
    number = randint(0,10)
    # loop three times to give at most three attempts
    for attempt in range(3):
        # cast to int, don't use eval
        guess = int(input("Guess 1: Please enter an integer between 1 and 10: "))
        if guess < number:
            print("Your guess is too small.")
        elif guess > number:
            print("Your guess is too big.")          
        else: # not higher or lower so must be the number  
            print("You got it!")
            break

最好使用 while 和 try/except 来验证用户输入的数字,循环直到用户尝试 3 次或猜对为止:

def guess():
    print ("I'm thinking of an integer, you have three guesses.")
    attempts = 0
    from random import randint
    number = randint(0,10)

    while attempts < 3:
        try:
            guess =int(input("Guess 1: Please enter an integer between 1 and 10: "))
        except ValueError:
            print("That is not a number")
            continue
        if guess < number:
            print("Your guess is too small.")
            attempts += 1
        elif guess > number:
            print("Your guess is too big.")
            attempts += 1
        else: # if it is a number and not too high or low it must be correct
            print("You got it!")
            break # break the loop

如果您真的想向用户反馈他们的猜测是太低还是太高,则不能只使用 if/else

也如评论所述不要使用 eval。概述了一些很好的理由 here

  1. 你所有的 while guess!=number and attempts == 循环都是无用的,因为你要么 break 退出它们,要么递增 attempts 所以它们的条件在第一个之后评估为 False迭代。
  2. 猜测 2 从未达到,因为 number 等于 guess(所以 number != guessFalse)或 attempts 仍然为零。
  3. 由于同样的原因,猜测 3 从未达到。但是,如果 猜测 2 会被达到,猜测 3 将 永远不会 被达到,因为你把 elif 放在前面。

尽量去掉猜2和猜3的代码,把guess = eval(input())if guess < number: ... elif guess > number: ...的代码全部写完一次放到里面一个循环。这里有一些伪代码来说明这个想法:

while attempts < 3
    ask for user input
    if guess equals number
        print "you win"
        exit the loop
    else
        print "that's wrong"

我使用了 "concatenation" 方法以及您的一些有用的回复想法,终于让我的代码工作了!!非常感谢大家,非常感谢您的帮助!!这是该程序的正确代码:

def guess():
from random import randint
number = randint(0,10)
print("I'm thinking of an integer, you have three guesses.")
attempts = 0
while attempts < 2:
    guess = eval(input("Guess " + str(attempts + 1) + ": Please enter an integer between 1 and 10: "))
    if guess < number:
        print("Your guess is too small.")
        attempts += 1
    elif guess > number:
        print("Your guess is too big.")
        attempts += 1
    else:
        print("You got it!")
        break

else:
    attempts == 3
    guess = eval(input("Guess 3: Please enter an integer between 1 and 10: "))
    if guess < number:
        print("Too bad. The number is: ", number)
    elif guess > number:
        print("Too bad. The number is: ", number)
    else:
        print("You got it!")

然后调用函数 ("guess()") 结束它。希望这对将来遇到此问题的人有用。再次谢谢大家!