【问题标题】:Suspected order of functions/while loop glitches game out可疑的函数顺序/while循环故障游戏
【发布时间】:2019-01-18 12:35:48
【问题描述】:

我希望我的代码...

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

  • 在游戏中:

  • 玩家下注和猜测。

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

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

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

  • 如果是,余额会随着奖金/损失而更新,生成新号码并更新余额。尝试也被重置。

  • 如果否,播放器将返回菜单。

  • 如果尝试== 0,则由于玩家输了,会再次出现“是/否”选择提示,并且余额会随着输掉而更新。

问题是……

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

  • 除了 1 件事外,一切正常:当游戏赢或输达到 0 次尝试时输入是/否,会发生这种情况:

图片:0

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

`

感谢您帮助新手!

【问题讨论】:

  • 您实际上并没有包含代码或图片。
  • 已更正,对此感到抱歉。我不确定发生了什么
  • 我使用的是 python 3.6

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


【解决方案1】:

问题是if choice in POSITIVES。您的 choice 变量始终指向 "\nPlay again? Y/N: " 字符串,并且播放器提供的选项实际上从未被“记录”。

要解决此问题,您应该

  1. 当您拨打 input(choice) 时保存玩家的答案 - 即 player_choice = input(choice)
  2. 检查此变量,即if player_choice in POSITIVES

【讨论】:

    【解决方案2】:

    您的问题在于这些电话:

    input(choice)
    

    应该是

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      • 2021-11-02
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多