【发布时间】:2019-01-18 12:35:48
【问题描述】:
我希望我的代码...
- 询问用户名并存储用户名
- 显示一个菜单屏幕,玩家必须 - 输入任意键才能开始游戏。
当玩家输入“任何提示”时生成数字并重置“尝试”
在游戏中:
玩家下注和猜测。
如果错了,回到猜测和下注。变量平衡和尝试减去赌注和 -1 尝试。
如果猜测与生成的数字相同,则赢得屏幕。玩家将获得添加到其余额中的可变奖金。
无论输赢都显示“选择”菜单,并提示玩家通过回答是或否来重新玩游戏。
如果是,余额会随着奖金/损失而更新,生成新号码并更新余额。尝试也被重置。
如果否,播放器将返回菜单。
如果尝试== 0,则由于玩家输了,会再次出现“是/否”选择提示,并且余额会随着输掉而更新。
问题是……
我怀疑函数的顺序和/或重新启动/结束游戏的循环有问题。
除了 1 件事外,一切正常:当游戏赢或输达到 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