python - 怀疑函数顺序/while 循环会导致游戏失败

标签 python python-3.x function while-loop

我希望我的代码...

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

  • 游戏中:

  • 玩家下注和猜测。

  • 如果错误,返回猜测并下注。变量 Balance 和 attempts 减去赌注和 -1 尝试。

  • 如果猜测与生成的数字相同,则赢得屏幕。玩家将获得添加到其余额中的可变奖励。

  • 无论输赢都会显示“选择”菜单,并提示玩家通过回答是或否来再次玩。

  • 如果是,则余额将根据奖金/损失进行更新,生成新号码并更新余额。尝试次数也会重置。

  • 如果否,玩家将返回菜单。

  • 如果 attempts == 0,则由于玩家输了,将再次出现“是/否”选择提示,并且余额会根据输掉的情况进行更新。

问题是...

  • 我怀疑函数的顺序和/或重新启动/结束游戏的循环顺序不正确。

  • 除了一件事之外,一切正常:当游戏获胜或因达到 0 次尝试而失败时输入是/否,会发生这种情况:

图片:0 enter image description here

我尝试过更改 game_state 变量,更改 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])     

`

感谢您对新手的帮助!

最佳答案

问题是如果选择积极。您的 choice 变量始终指向 "\nPlay Again? Y/N: " 字符串,而播放器提供的选项实际上从未“记录”。

要解决此问题,您应该

  1. 在调用 input(choice) 时保存玩家答案 - 即 player_choice = input(choice)
  2. 检查此变量,即player_choice是否为正

关于python - 怀疑函数顺序/while 循环会导致游戏失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54254174/

相关文章:

javascript - this.element 在对象构造函数内部无法识别

c# - 将 UINT64 编码为 float

Python Dict 多个 Max 值

python - 重新编译 python 3 包时未拾取本地编译的 libffi 文件

javascript - 如何在页面加载时自动运行功能?

javascript - 我的滚动事件没有触发我的 javascript 函数

python - 在 python 中比较 `list`

Python 端口监听器(如 NC)

python - 分段函数: Find equation and solve it using data points

python - 如何计算python3中defaultdict的唯一记录