简体   繁体   English

错误的猜谜游戏程序

[英]Bug with a program for a guessing game

I am creating a program where a user had to guess a random number in a specific range and has 3 tries to do so. 我正在创建一个程序,用户必须猜测一个特定范围内的随机数,并尝试进行3次。 After each try, if the guess is correct you tell the user they won and if the guess is wrong you tell them its wrong and how many tries they have got remaining. 每次尝试后,如果猜测是正确的,则告诉用户他们赢了;如果猜测是错误的,则告诉他们错误,以及还剩下多少次尝试。 When the number of tries exceed 3 without a correct guess, you tell the user they are out of tries and tell them they lost. 如果尝试次数超过3次而没有正确的猜测,则您会告诉用户尝试次数过少,并告诉他们失败了。 When the program finishes after a correct or 3 wrong tries and the game is over, you ask if the user wants to play again. 在正确的尝试或3次错误尝试后程序结束并且游戏结束时,您询问用户是否要再次玩。 If they say "yes", you star the game again and if they say "no", you end the game. 如果他们说“是”,则您再次注视游戏;如果他们说“否”,则结束游戏。 This is the code I have come up with: 这是我想出的代码:

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

Now when you guess the correct number before 3 tries, the program seems to be working fine, but even when the number of wrong tries exceeds 3, the program keeps asking for more guesses but I want it to stop the program right there. 现在,当您在尝试3次之前猜测正确的数字时,该程序似乎可以正常运行,但是即使错误尝试的次数超过3次,该程序仍会继续要求更多猜测,但我希望它在此停止该程序。 The second thing I don't get is how do I start the whole game again when the user wants to play again? 我没有得到的第二件事是,当用户想再次玩游戏时,如何重新开始整个游戏? How can I modify my code to fix these errors? 如何修改代码以解决这些错误?

I guess this is it. 我猜就是这样。 Tested this out. 测试了这一点。 Working Perfect (Y) 工作完美(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!"

Hope this helps. 希望这可以帮助。

Here's more compact code you can try. 您可以尝试以下更紧凑的代码。 Also it is solving your "Wrong Guess. Try Again." 它还正在解决您的“错误猜测。重试”。 issue on last guess. 最后猜测的问题。

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

The simplest way to stop a loop when some condition becomes true is to use break . 在某些条件变为真时停止循环的最简单方法是使用break So when the player guesses correctly, the code would look like 因此,当玩家猜对时,代码看起来像

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

Additionally, you should break the loop when you run out of guesses, otherwise it will keep running 此外,当您用尽所有猜测时,应打破循环,否则它将继续运行

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

To let the player play again, you can have a variable playing and use it in a while loop that wraps the whole game, and when they say they don't want to play anymore, make it False so that the outer loop ends. 要让玩家再次玩游戏,您可以playing一个变量,并在包装​​整个游戏的while循环中使用它,当他们说不想再玩时,将其设置为False ,以使外循环结束。

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

As you noted in comments on another answer, when the player gets their third guess wrong, the game tells them they have 0 guesses left and to try again, and then tells them they are out of tries and that the game is over. 正如您在对另一个答案的评论中所指出的那样,当玩家第三次猜错了游戏时,游戏会告诉他们他们还有0个猜想,然后再试一次,然后告诉他们他们没有进行尝试并且游戏结束了。 Since you want it to only tell them they are out of tries and that the game is over, you can change the main block of code to: 由于您只希望告诉他们他们已经尝试过并且游戏结束了,因此您可以将主要代码块更改为:

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)

this is a slightly different version to your problem: 这与您的问题稍有不同:

import random

def play():
    x = round(random.random()*5)
    guess = False
    tries = 0
    while guess is False and tries < 3:
        inp = raw_input("Guess the number: ")
        if int(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 == 3:
                print("Game Over!")

play()
again = "maybe"
while again != "no":
    again = raw_input("Do you want to play again? yes OR no? \n")
    if again == "yes":
        print "Great!"
        play()
print "Okay thanks for playing!"

You need to make guess equal to True in your second else or else it will never satisfy stop condition for while loop. 你需要做的猜测等于True在你的第二个else ,否则它永远不会满足,而循环停止条件。 Do you understand? 你理解吗?

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

EDIT: I think this does the trick using your code. 编辑:我认为这可以使用您的代码来解决问题。

You need to move where you set guess = True 您需要将猜测值设置为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!"

Before you were only ending on the condition where the last guess was correct. 在您仅以最后一个猜测正确的条件结束之前。 You should be ending whether the guess was correct or not. 无论猜测是否正确,您都应该结束。

You need to update tries. 您需要更新尝试。 Additionally you need to break out of your loop even if guess != True 此外,即使您猜出!= 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!"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM