I've been driving myself insane trying to troubleshoot and fix this. I cerated this dice rolling game with bets. I'm pretty new to coding, so I'm sure it's a simple error that I just keep overlooking.
- The only issue is that it will not end the game if the user inputs "no".
- The function doesn't return any errors. It runs and even prints GAME OVER before it returns the values used to find out if the player wants to play another round.
- What happens is that it prints the game over, and then loops the diceRoll function without stopping it.
Picture uploaded to show what I mean.
And the code:
#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.