0

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. enter image description here

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.
Keyan
  • 87
  • 1
  • 11
  • 1
    `if (balance <= 0 or choice == "YES" or "Y"):` This doesn't do what you think it does. :-) – TrebledJ Jan 14 '19 at 10:27
  • 1
    `'Y'` will always evaluate to `True` and it will never enter the `else` portion, is what Treb is saying – ycx Jan 14 '19 at 10:30

2 Answers2

2

Inside diceRoll() function this should be changed from:

if (balance <= 0 or choice == "YES" or  "Y")

to

if (balance <= 0 or choice == "YES" or choice == "Y")

to properly compare with the choice value.

In your case to make it clearer you are having 3 condition:

  • balance <= 0
  • choice == "YES"
  • "Y"

with the third one being always True. It doesn't check if choice has Y value but if the string provided "Y" in your case is equal to None or not and obviously it's not so it's always True.

Eypros
  • 5,370
  • 6
  • 42
  • 75
1

In addition to the answers given, I recommend a list of positive choice strings and check if choice is in it.

acceptables = ["yes", "y", "yup", "yeah", "sure", "absolutely", "1", "roger"]

if (balance <= 0 or choice.lower() in acceptables)

.lower() converts the input string into lower case, so you don't have to bother about that. If you want to allow more than the examples in the above case, you can always add them as you wish. Same for, "no", "nope", "no way", "never", "0", ...

offeltoffel
  • 2,691
  • 2
  • 21
  • 35