用不同的猜测名称猜数字游戏

时间:2015-01-26 12:04:04

标签: python python-3.x

这是我第一次使用stackoverflow访问 - 我是编程新手,也是Python的初学者课程。兴奋开始!

我们的第二项任务要求我们创建着名的Guess the Number Game。对于那些已经了解这个游戏的人,我会喜欢一些已添加到其中的额外部分的帮助:我们必须用他们各自的订单列出每个猜测。示例输出应如下所示:

  • 我在想一个整数,你有三个猜测。
  • 猜1:请输入1到10:4之间的整数
  • 你的猜测太小了。
  • 猜猜2:请输入1到10:8之间的整数
  • 你的猜测太大了。
  • 猜3:请输入1到10:7之间的整数
  • 太糟糕了。数字是:5

我已经将编码下载到我猜猜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然后退出。任何人都可以帮我弄清楚如何让猜猜2和3出现?欢迎所有想法 - 谢谢!

3 个答案:

答案 0 :(得分:2)

您可以稍微缩短代码,只需在循环中移动输入并使用范围保持循环三次尝试或用户猜测正确:

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

最好使用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 :(得分:0)

  1. 您的所有while guess!=number and attempts ==循环都无用,因为您break要么attempts要么递增False所以他们的条件在第一次后评估为number迭代。
  2. 永远不会到达猜猜2,因为guess等于number != guess(因此Falseattempts)或elif仍然为零。
  3. 出于同样的原因,从未达到过猜测3。但是,如果猜测2将会达到,则猜测3将从不,因为您将guess = eval(input())放在前面。
  4. 尝试摆脱猜测2的代码并猜测3.编写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" 的所有代码并将其放入循环中。这里有一些伪代码来说明这个想法:

    {{1}}

答案 2 :(得分:0)

我使用了&#34;连接&#34;方法以及一些有用的响应想法,最后让我的代码工作!非常感谢大家的帮助!!以下是此程序的正确代码:

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!")

然后通过调用函数结束它(&#34; guess()&#34;)。希望这对那些将来遇到这个问题的人有用。再次,谢谢你们!