Loop issue and odd code in my Python slot machine

589 Views Asked by At

i have already read all the other slot machine post, and i can understand the theory of the loop and get all the other solutions, until the code is simple and short.
my array is so bad at the moment that i can't really figure out why i can't play more than one time

here my entire array, it include the help came for other questions i have opened in the past 2 days.

please be sincere and rude if this code sucks:

import random
import time

cr = 100
print('[- play simple slot machine game -]')
time.sleep(1)
print('[- with five identical n you win -]')
time.sleep(1)
print('[- start at ' + str(cr) + 'cr place your bet -]')
time.sleep(1)
slot = []
for row in range(9):
    slot.append([None] * 9)


def print_slot(slot, empty='[ ]'):
    for row in slot:
        print(' '.join(empty if element is None else '{{: ^{}}}'.format(len(empty)).format(element) for element in row))

print_slot(slot)

time.sleep(1)
print('[- play simple slot machine game -]')
time.sleep(1)
print('[- with five identical n you win -]')
time.sleep(1)


def game_start():
    cr = 100
    bet = int(input('[- digit the num of coins to bet -] \n                 '))
    if (cr > 0) and (0 < bet <= cr):
        cr = 100 - bet
        first_n = random.randint(0, 9)
        second_n = random.randint(0, 9)
        third_n = random.randint(0, 9)
        fourth_n = random.randint(0, 9)
        fifth_n = random.randint(0, 9)
        slot[4][2] = str(first_n)
        slot[4][3] = str(second_n)
        slot[4][4] = str(third_n)
        slot[4][5] = str(fourth_n)
        slot[4][6] = str(fifth_n)

        if first_n == second_n and second_n == third_n and third_n == fourth_n and fourth_n == fifth_n:
            cr += bet * 100
            time.sleep(1)
            print('[- play simple slot machine game -]')
            time.sleep(1)
            print('[- with five identical n you win -]')
            time.sleep(1)
            print_slot(slot)
            time.sleep(1)
            print('[- with five identical n you win -]')
            time.sleep(1)
            print('[- you won!!! now you have ' + str(cr) + 'cr -]')
            bet = int(input('[- digit the num of coins to bet -] \n                 '))
            return bet

        else:
            time.sleep(1)
            print('[- play simple slot machine game -]')
            time.sleep(1)
            print('[- with five identical n you win -]')
            time.sleep(1)
            print_slot(slot)
            time.sleep(1)
            print('[- with five identical n you win -]')
            time.sleep(1)
            print('[- you loose! now you have  ' + str(cr) + 'cr -]')
            bet = int(input('[- digit the num of coins to bet -] \n                 '))
            return bet

    elif cr < 0:
        time.sleep(1)
        print('[- play simple slot machine game -]')
        time.sleep(1)
        print('[- with five identical n you win -]')
        time.sleep(1)
        print_slot(slot)
        time.sleep(1)
        print('[- with five identical n you win -]')
        time.sleep(1)
        print('[- you ran out of money 2 bet :( -]')

game_start()
3

There are 3 best solutions below

0
On BEST ANSWER

You have to -= the credit each time there is a bet and just remove the elif, I removed some unnecessary lines, added a try/except to cast to an int and catch bad input and a way for the user to quit the game. You can add a sleep or two where you like but it should run how you want:

def game_start():
    cr = 100
    while cr > 0:
        bet = input('[- digit the num of coins to bet -] \n or enter q to quit')
        if bet == "q": # if user wants to quit they can enter q
            print("Thanks for playing, you are leaving with ${}".format(cr))
            return # leaves/ends the function
        try: # use try/except to cast to int to catch illegal input
           bet = int(bet)
        except ValueError:
            print("Invalid input")
            continue
        if bet <= cr: # make sure we have enough credit
            cr -= bet
            first_n = random.randint(0, 9)
            second_n = random.randint(0, 9)
            third_n = random.randint(0, 9)
            fourth_n = random.randint(0, 9)
            fifth_n = random.randint(0, 9)
            slot[4][2] = str(first_n)
            slot[4][3] = str(second_n)
            slot[4][4] = str(third_n)
            slot[4][5] = str(fourth_n)
            slot[4][6] = str(fifth_n)
        else: # if we get here we tried to bet too much so use continue to go back to start of loop and ask for bet again
            print("You don't have that much credit")
            continue
        # if we get here all is good 
        if len({first_n,second_n,third_n,fourth_n,fifth_n}) == 1: # sets don't have dups so if we have five that are equal we will only have one element
                cr += bet * 100
                time.sleep(1)
                print('[- play simple slot machine game -]')
                time.sleep(1)
                print('[- with five identical n you win -]')
                time.sleep(1)
                print_slot(slot)
                time.sleep(1)
                print('[- with five identical n you win -]')
                time.sleep(1)
                print('[- you won!!! now you have ' + str(cr) + 'cr -]')
        else: # if we did not have a winning spin we will get here
            print("Hard Luck, You have {} credit left".format(cr))
    # this will print if we run out of credit        
    print('[- you ran out of money 2 bet :( -]')

cr = 100 - bet keeps setting cr - equal to 100 - the current bet, it does not update the cr by decrementing by the bet amount.

Having a return bet in your code will cause the function to end when you reach that statement no matter how much credit you have, you only want to break/return when you have 0 dollars left or the user wants to leave.

9
On

There is no point returning bet, since you don't do anything with the return value. Also, you can't do anything in the function after you return from it - it's like telling your kid to shut the door when he's already at the playground. Putting a loop in is the correct solution. Also, if you keep the constant 100 in the loop, it will never terminate unless the player inputs 100 or 0. (Also, "loose" is opposite of "tight"; opposite of "win" is "lose".

import random
import time

cr = 100
print('[- play simple slot machine game -]')
time.sleep(1)
print('[- with five identical n you win -]')
time.sleep(1)
print('[- start at ' + str(cr) + 'cr place your bet -]')
time.sleep(1)
slot = []
for row in range(9):
    slot.append([None] * 9)


def print_slot(slot, empty='[ ]'):
    for row in slot:
        print(' '.join(empty if element is None else '{{: ^{}}}'.format(len(empty)).format(element) for element in row))

print_slot(slot)

time.sleep(1)
print('[- play simple slot machine game -]')
time.sleep(1)
print('[- with five identical n you win -]')
time.sleep(1)


def game_start():
    cr = 100
    bet = int(input('[- digit the num of coins to bet -] \n                 '))
    while (cr > 0) and (0 < bet <= cr):
        cr = cr - bet
        first_n = random.randint(0, 9)
        second_n = random.randint(0, 9)
        third_n = random.randint(0, 9)
        fourth_n = random.randint(0, 9)
        fifth_n = random.randint(0, 9)
        slot[4][2] = str(first_n)
        slot[4][3] = str(second_n)
        slot[4][4] = str(third_n)
        slot[4][5] = str(fourth_n)
        slot[4][6] = str(fifth_n)

        if first_n == second_n and second_n == third_n and third_n == fourth_n and fourth_n == fifth_n:
            cr += bet * 100
            time.sleep(1)
            print('[- play simple slot machine game -]')
            time.sleep(1)
            print('[- with five identical n you win -]')
            time.sleep(1)
            print_slot(slot)
            time.sleep(1)
            print('[- with five identical n you win -]')
            time.sleep(1)
            print('[- you won!!! now you have ' + str(cr) + 'cr -]')
            bet = int(input('[- digit the num of coins to bet -] \n                 '))

        else:
            time.sleep(1)
            print('[- play simple slot machine game -]')
            time.sleep(1)
            print('[- with five identical n you win -]')
            time.sleep(1)
            print_slot(slot)
            time.sleep(1)
            print('[- with five identical n you win -]')
            time.sleep(1)
            print('[-  you lose! now you have ' + str(cr) + 'cr  -]')
            bet = int(input('[- digit the num of coins to bet -] \n                 '))

    if cr <= 0:
        time.sleep(1)
        print('[- play simple slot machine game -]')
        time.sleep(1)
        print('[- with five identical n you win -]')
        time.sleep(1)
        print_slot(slot)
        time.sleep(1)
        print('[- with five identical n you win -]')
        time.sleep(1)
        print('[- you ran out of money 2 bet :( -]')

game_start()
3
On

I think when you are returning bet you are bringing yourself out of your entire game_start() function. This would explain why calling game_start after returning bet wouldn't work (the program would end with the return statement!).

I think combining what you said you tried with the while loop inside the function (but after you initialize cr to 100) and removing the return statements should work. Just change the value of bet, but don't return it.

In other words, just do what @Amadan said (was typing when he posted, and I missed the 100 - bet error)