疑似函数顺序/while循环故障

2024-10-01 09:32:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望我的代码

  • 询问用户名和存储用户名
  • 显示一个菜单屏幕,玩家必须-输入任何键开始游戏
  • 当玩家输入“任何提示”时生成数字并重置“尝试”

  • 游戏中:

  • 玩家下注和猜测

  • 如果错了,回到猜测和打赌。变量平衡并尝试减去赌注和-1尝试

  • 如果猜测和生成的数字相同,则赢得屏幕。玩家得到的可变奖金加在他的余额上

  • 赢/输都会显示“选择”菜单,并通过回答“是”或“否”提示玩家重新开始游戏。

  • 如果是,则用奖品/损失更新余额,生成新号码并更新余额。尝试也被重置

  • 如果没有,玩家被送回菜单

  • 如果tries==0,那么“yes/no”选项提示将再次出现,因为玩家输了,余额将更新为输了

问题是

  • 我怀疑函数的顺序和/或重新启动/结束游戏的循环有问题

  • 除了一件事之外,所有的事情都能正常工作:当游戏赢了或输了0次时,输入yes/no,这种情况就会发生:

图:0enter image description here

我尝试过改变游戏的状态变量,改变if/elif语句,甚至尝试过添加更多的函数/while循环,但都不适合我

我对Python还不熟悉,已经到了极限

我的代码:

#pylint:disable=W0613
#pylint:disable=W0312
#pylint:disable=W0611
from random import randint
import math
######### NUMBER GUESSING GAME ##########

START_BALANCE = 500

POSITIVES = ["yes", "yeah", "y", "yep", "roger", "yea", "positive", "play"]
NEGATIVES = ["no", "nope", "n", "nah", "negative"]

choice = ("\nPlay again? Y/N:     ").upper()
userName = input ("Welcome to NumGuess! What is your name?\n")
userName = userName.title()

def menu():
    print(''' \n                        Hello {}!\n
                * The rules are very simple *
--         The AI generates a number from 1 - 100.       --
--    You will have to make a bet and enter your guess.  --
--   You have 10x tries. If you fail, you lose your bet. --
--   The AI will let say if you guessed 'low' or 'high'  --
--    Correct guess = prize. Wrong guess = lost bet.     --

                       - Good Luck! - 
'''.format(userName))

def menuPlay():

    try:
        menuPlay = input("Press any key to start the game.")
#   except (ValueError):
    #   return menuPlay()
    except TypeError:
        return menuPlay()
    else:
        if menuPlay.upper() != "":
            return

def xNumbers():
    number = randint(1,100)
    return number

def xTries():
    tries = 3
    return tries

def xBets():
    print("-------------------------------------")
    bet = int(input("Enter your bet:     "))
    return bet

def xGuesses():
    guess = int(input("Enter your guess:    "))
    return guess


menu()
menuPlay()
tries = xTries() 
number = xNumbers()

def main(tries, balance):
    print("\nYour balance is: {}$.\nYou have {}x tries left.\n".format(balance, tries))
    bet = xBets()
    guess = xGuesses()

    print("\nnumber: {}, guess: {}, bet: {}".format(number, guess, bet)) ##just to check if things are working

    if tries <=1:
        print("\nGAME OVER! - YOU ARE OUT OF TRIES!\n - The number was: {}.".format(number))
        input(choice)
        return [balance]

    if guess == number:
        prize = bet * float(3.75)
        prize = math.ceil(prize)
        balance += prize
        print("Congratulations! You win: {}$".format(prize))
        print("Your new balance is: {}$\n".format(balance))

    elif guess < number:
        print("Wrong guess!")
        print("- Your guess is too low!")
        tries -= 1
        balance -= bet
        main(tries, balance)
    elif guess > number:
        print("Wrong guess!")
        print("- Your guess is too high!")
        tries -= 1
        balance -= bet
        main(tries, balance)    

    player_Choice = input(choice)

    if player_Choice in POSITIVES: #If player wants to play again.
        print("New round started!")
        return [True, balance] #return True & updated balancd to while loop.

    else: # If player inputs NO to play again.
        print("\nThanks for playing!\n")
        return [False, balance] #return False & updated balnce to while loop - should end the game.
        # BONUS: If this could return to menuPlay() with an updated balance, that would be ideal.


game_state = [True, START_BALANCE]

while game_state[0]:
    game_state = main(tries, game_state[1])     

`

谢谢你帮助一个新手


Tags: to游戏numberinputreturnifdef玩家
2条回答

问题是if choice in POSITIVES。您的choice变量总是指向"\nPlay again? Y/N: "字符串,而player提供的选项实际上从未“记录”

要解决这个问题,你应该

  1. 当您呼叫input(choice)-即player_choice = input(choice)时,保存播放器应答
  2. 检查此变量,即if player_choice in POSITIVES

你的问题在于这些电话:

input(choice)

应该是的

choice = input("\nPlay again? Y/N:     ")

您的代码使用变量choice来表示提示和用户对提示的响应(if choice in POSITIVES:

相关问题 更多 >