功能的疑似顺序/ while循环故障导致游戏中断

时间:2019-01-18 12:35:49

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

我要输入我的代码...

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

  • 在游戏中:

  • 玩家下注和猜测。

  • 如果有误,请返回猜测并下注。变量平衡并尝试减去下注和-1 try。

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

  • 获胜/失败都显示“选择”菜单,并通过回答“是”或“否”提示玩家再次玩游戏。

  • 如果是,则用奖金/亏损更新余额,生成新的数字并更新余额。尝试也会重置。

  • 如果否,则将播放器发送回菜单。

  • 如果尝试== 0,则将再次出现“是/否”选择提示,因为玩家输了,余额随着损失而更新。

问题在于...

  • 我怀疑功能的顺序和/或重新启动/结束游戏的循环不正常。

  • 除了1件事外,其他所有工作都可以:当游戏赢了或输了0次而输了,则输入yes / no,就会发生这种情况:

图片: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])     

`

感谢您帮助新手!

2 个答案:

答案 0 :(得分:2)

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

要解决此问题,您应该

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

答案 1 :(得分:1)

您的问题出在以下电话中:

input(choice)

应该是

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

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