我一直在疯狂地尝试解决并解决此问题。我用赌注证明了这个骰子滚动游戏。我对编码非常陌生,因此可以肯定,这是一个我一直忽略的简单错误。
上传图片以显示我的意思。
和代码:
#pylint:disable=W0312
#python 3.6
from random import randint
import math
# Static Variables
START_BALANCE = 2500
# Game messages
BAD_GUESS = "ERROR! You can only enter a number between 1 and 6.\n- Your input was '{}'.\n"
BAD_BET = "ERROR! You can not bet less than 0 or more than your current balance {}$.\n"
userName = input("What is your name, player?\n").title()
print("Hello, {}! Welcome to the dice rolling game v.2!\n".format(userName))
def get_guess():
try:
guess = input("Enter a guess from 1 to 6, {}\n".format(userName))
guess = int(guess)
while (guess <= 0 or guess > 6):
print(BAD_GUESS.format(guess))
return get_guess()
except ValueError:
print(BAD_GUESS.format(guess))
return get_guess()
else:
print("Your guess is: {}.".format(guess))
return guess
def get_bet(balance):
try:
bet = input("\nHow much do you want to bet? Your balance is: {}$\n".format(balance))
bet = int(bet)
while (balance < bet) or (bet < 0):
print(BAD_BET.format(balance))
return get_bet(balance)
except ValueError:
print(BAD_BET.format(balance))
return get_bet(balance)
else:
print("You have bet {}$!\n- Your remaining balance is: {}$".format(bet, balance - bet))
return bet
#### FUNC START ####
def diceRoll(balance):
bet = get_bet(balance)
guess = get_guess()
balance -= bet
roll = randint(1,6)
if (roll == guess):
prize = bet * float(3.75)
prize = math.ceil(prize)
balance += prize
print("\nYOU WIN {}$!".format(prize))
print("You guessed: {} - The roll was {}!\n".format(guess, roll))
print("-- Your balance is now {}$ -- \n".format(balance))
elif (roll != guess):
print("\nYOU LOSE {}$".format(bet))
print("The roll was: {} - You guessed: {}.".format(roll,guess))
print("-- Your balance is now {}$ --\n".format(balance))
#
choice = input("Would you like to try again? Y/N\n").upper()
#
if (balance <= 0 or choice == "YES" or "Y"):
print("New round! Your balance is {}$".format(balance))
return [True, balance]
else:
print("GAME OVER! \n Balance: {}$".format(balance))
return [False, balance]
# Initialize game_state, which is a variable that keeps track of your rounds and balance.
game_state = [True, START_BALANCE]
# game_state[0] contains True if the user wants to play again, False if not.
# So if it's false, while (game_state[0]) will evaluate to false and stop the while loop.
while game_state[0]:
game_state = diceRoll(game_state[1])
# TODO: Build a while loop that executes any time the first item in game_state is True (i.e., while the
# user still wants to play a new round. Note that I initialized game_state[0] to True, assuming that
# if they started the program, they want to play the first round.
答案 0 :(得分:2)
在diceRoll()
函数内部,应更改为:
if (balance <= 0 or choice == "YES" or "Y")
到
if (balance <= 0 or choice == "YES" or choice == "Y")
正确地与choice
值进行比较。
为清楚起见,您有3种情况:
balance <= 0
choice == "YES"
"Y"
,第三个总是True
。它不会检查choice
是否具有Y
的值,但是会检查您提供的"Y"
是否等于None
的字符串,并且显然不是,因此始终为True。
答案 1 :(得分:1)
除了给出的答案外,我还建议列出肯定选择字符串,并检查其中是否包含choice
。
acceptables = ["yes", "y", "yup", "yeah", "sure", "absolutely", "1", "roger"]
if (balance <= 0 or choice.lower() in acceptables)
.lower()
将输入字符串转换为小写,因此您不必为此而烦恼。如果您不希望在上述情况下提供更多示例,则可以随时添加它们。相同于“不”,“不”,“不行”,“从不”,“ 0”,...