用于猜谜游戏的程序的错误

时间:2016-10-06 17:39:53

标签: python python-2.7

我正在创建一个程序,其中用户必须猜测特定范围内的随机数,并且有3次尝试这样做。在每次尝试之后,如果猜测是正确的,你告诉用户他们赢了,如果猜测是错误的,你告诉他们错了,他们剩下多少次尝试。当没有正确猜测的尝试次数超过3时,您告诉用户他们没有尝试并告诉他们他们丢失了。当程序在正确或3次错误尝试完成并且游戏结束后完成时,您会询问用户是否想要再次玩游戏。如果他们说“是”,你再次为游戏加注,如果他们说“不”,你就会结束游戏。 这是我提出的代码:

import random
x =round(random.random()*5)
guess = False
tries = 0
while guess != True:
    if tries<=3:
        inp = input("Guess the number: ")
        if tries<3:
            if inp == x:
                guess = True
                print "Correct! You won"
            else:
                print "Wrong guess! Try again."
                tries = tries + 1
                print "Your remaining tries are",(3-tries)
        if tries>2:
            if inp == x:
                guess = True
                print "Correct! You won"
            else:
                print "Wrong guess! You are out of tries"
                print "Game Over!"
again = raw_input("Do you want to play again? ")
if again == "no":
    print "Okay thanks for playing!"
elif again =="yes":
    print "Great!"

现在当你在3次尝试之前猜到正确的数字时,程序似乎工作正常,但即使错误尝试的次数超过3次,程序仍然要求更多的猜测,但我希望它能在那里停止程序。我没有得到的第二件事是当用户想再次玩时如何重新开始整个游戏? 如何修改我的代码以修复这些错误?

6 个答案:

答案 0 :(得分:3)

我猜是这样的。测试了这一点。完美工作(Y)

import random
x =round(random.random()*5)
guess = False
tries = 0
while guess != True:
    if tries<=3:
        inp = input("Guess the number: ")
        if tries<3:
            if inp == x:
                guess = True
                print "Correct! You won"
            else:
                print "Wrong guess! Try again."
                tries = tries + 1
                print "Your remaining tries are",(3-tries)
        if tries>2:
            if inp == x:
                guess = True
                print "Correct! You won"
            else:
                print "Wrong guess! You are out of tries"
                print "Game Over!"
                again = raw_input("Do you want to play again? ")
                if again == "no":
                    guess = True
                    print "Okay thanks for playing!"
                elif again =="yes":
                    tries = 0
                    print "Great!"

希望这有帮助。

这是您可以尝试的更紧凑的代码。它也解决了你的“错误的猜测。再试一次。”最后猜测的问题。

import random
x =round(random.random()*5)
guess = False
tries = 0
print x
while guess != True:
    while tries<3:
        inp = input("Guess the number: ")
        if inp == x:
            guess = True
            print "Correct! You won"
            break
        else:
            tries = tries + 1
            if tries == 3:
                break
            print "Wrong guess! Try again."
            print "Your remaining tries are", (3 - tries)

        if not guess:
            print "Wrong guess! You are out of tries"
            print "Game Over!"
        again = raw_input("Do you want to play again? ")
        if again == "no":
            guess = True
            print "Okay thanks for playing!"
        elif again == "yes":
            tries = 0
            if guess:
                guess = False
                x = round(random.random() * 5)
            print "Great!"

答案 1 :(得分:3)

当某些条件变为真时停止循环的最简单方法是使用break。因此,当玩家猜对时,代码看起来像

if inp == x:
    guess = True
    print "Correct! You won"
    break

此外,当你用完猜测时你应该打破循环,否则它将继续运行

else:
    print "Wrong guess! You are out of tries"
    print "Game Over!"
    break

为了让玩家再次玩,你可以拥有一个变量playing并在一个包裹整个游戏的while循环中使用它,当他们说他们不想再玩时,让它False 1}}以便外循环结束。

playing = True
while playing:
    x = round(random.random()*5)
    guess = False
    tries = 0
    while guess != True:
        ...

    again = raw_input("Do you want to play again? ")
    if again == "no":
        print "Okay thanks for playing!"
        playing = False
    elif again =="yes":
        print "Great!"

正如你在另一个答案的评论中所指出的那样,当玩家第三次猜错时,游戏会告诉他们他们有0猜测并再次尝试,然后告诉他们他们没有尝试并且游戏已经结束。既然你希望它只告诉他们他们没有尝试并且游戏结束了,你可以将主要的代码块更改为:

while guess != True:
    inp = input("Guess the number: ")
    if inp == x:
        guess = True
        print "Correct! You won"
    else:
        tries = tries + 1
        if tries == 3:
            print "Wrong guess! You are out of tries"
            print "Game Over!"
            break
        else:
            print "Wrong guess! Try again."
            print "Your remaining tries are",(3-tries)

答案 2 :(得分:2)

这与您的问题略有不同:

var family = [{ name: "Mike", age: 10 }, { name: "Matt", age: 13 }, { name: "Nancy", age: 15 }, { name: "Adam", age: 22 }, { name: "Jenny", age: 85 }, { name: "Nancy", age: 2 }, { name: "Carl", age: 40 }],
    unique = family.filter((set => f => !set.has(f.name) && set.add(f.name))(new Set));

console.log(unique);

答案 3 :(得分:1)

您需要在第二个True中使猜测等于else,否则它将永远不会满足while循环的停止条件。你明白了吗?

import random
x =round(random.random()*5)
guess = False
tries = 0
while guess != True:
    if tries<=3:
        inp = input("Guess the number: ")
        if tries<3:
            if inp == x:
                guess = True
                print "Correct! You won"
            elif tries+1 != 3:
                print "Wrong guess! Try again."
                print "Your remaining tries are",(3-(tries+1))
            tries = tries + 1    
        if tries>2:
            if inp == x:
                guess = True
                print "Correct! You won"
            else:
                print "Wrong guess! You are out of tries"
                print "Game Over!"
                guess= True

                again = raw_input("Do you want to play again? ")
                if again == "no":
                    print "Okay thanks for playing!"
                elif again =="yes":
                    print "Great!"
                    guess = False
                    tries = 0

编辑:我认为这可以解决使用您的代码的问题。

答案 4 :(得分:1)

您需要移动设置guess = True

的位置
import random
x =round(random.random()*5)
guess = False
tries = 0
while guess != True:
    if tries<=3:
        inp = input("Guess the number: ")
        if tries<3:
            if inp == x:
                guess = True
                print "Correct! You won"
            else:
                print "Wrong guess! Try again."
                tries = tries + 1
                print "Your remaining tries are",(3-tries)
        if tries>2:
            guess = True
            if inp == x:   
                print "Correct! You won"
            else:
                print "Wrong guess! You are out of tries"
                print "Game Over!"
again = raw_input("Do you want to play again? ")
if again == "no":
    print "Okay thanks for playing!"
elif again =="yes":
    print "Great!"

在你结束最后一次猜测正确的情况之前。你应该结束猜测是否正确。

答案 5 :(得分:0)

您需要更新尝试。另外,即使猜猜你也需要打破循环!= True

import random
x =round(random.random()*5)
guess = False
tries = 0
while guess != True:
    if tries<=3:
        inp = input("Guess the number: ")
        if tries<3:
            if inp == x:
                guess = True
                print "Correct! You won"
            else:
                print "Wrong guess! Try again."
                tries = tries + 1
                print "Your remaining tries are",(3-tries)
        if tries>2:
            if inp == x:
                guess = True
                print "Correct! You won"
            else:
                print "Wrong guess! You are out of tries"
                print "Game Over!"
                break
    tries += 1
again = raw_input("Do you want to play again? ")
if again == "no":
    print "Okay thanks for playing!"
elif again =="yes":
    print "Great!"