猜谜游戏程序的错误 [英] Bug with a program for a guessing game

查看:49
本文介绍了猜谜游戏程序的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个程序,用户必须在其中猜测特定范围内的随机数,并且有 3 次尝试这样做.每次尝试后,如果猜对了,你就告诉用户他们赢了,如果猜错了,你告诉他们错了,他们还剩下多少次尝试.当尝试次数超过 3 次而没有正确猜测时,您会告诉用户他们没有尝试并告诉他们他们输了.当程序在正确或 3 次错误尝试后结束并且游戏结束时,您会询问用户是否想再次玩游戏.如果他们说是",您再次开始游戏,如果他们说不",您就结束游戏.这是我想出的代码:

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

现在,当您在 3 次尝试前猜对了正确的数字时,该程序似乎运行良好,但即使错误尝试的次数超过 3 次,该程序仍不断要求更多的猜测,但我希望它立即停止程序.我不明白的第二件事是,当用户想再次玩游戏时,我该如何重新开始整个游戏?如何修改我的代码以修复这些错误?

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. 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?

推荐答案

我想就是这样.对此进行了测试.工作完美(Y)

I guess this is it. Tested this out. Working Perfect (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!"

希望这会有所帮助.

您可以尝试更紧凑的代码.它还可以解决您的错误猜测.再试一次".最后猜测的问题.

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

这篇关于猜谜游戏程序的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆