Answer to Question #343853 in Python for saif

Question #343853

Write a Python program that simulates a simple slot machine in which three numbers between 0 and 9 are randomly selected and printed side by side. The user has an initial sum of $100. The user enters his/her bet (make sure it is a valid bet, i.e., the bet is positive and less than or equal to the sum). If the three numbers are the same, the user hits the Jackpot, this means that he wins ten times his/her bet. If only two of the three numbers are the same the user wins four times his/her bet. After each round, the program outputs the sum that the user has and asks the user if he/she wish to continue playing. If yes the program repeats, else, the program displays the total winnings or losses of the user and then ends.


1
Expert's answer
2022-05-23T13:25:20-0400
from random import randrange as r


print('Welcome to the slot machine!')
print()
balance = 100
win = 0
lose = 0


print('Your balance:', balance)


while True:
    numbers = [r(0, 10) for i in range(3)]
    while True:
        bet = input('Please, input your bet that less or equal that your balance => ').strip()
        if bet.isdigit() and 0 <= int(bet) <= balance:         
            bet = int(bet)
            break
        else:
            print('You should input number!')


    print()


    if numbers[0] == numbers[1] == numbers[2]:
        win += 1
        bet *= 10
        balance += bet
        print(*numbers)
        print()
        print('Jackpot!!! (x10 to your bet)')
        print('Your winnings =>', bet)
        print()
        print('Your balance:', balance)
        print()
    elif numbers[0] == numbers[1] or numbers[0] == numbers[2] or numbers[1] == numbers[2]:
        win += 1
        bet *= 4
        balance += bet
        print(*numbers)
        print()
        print('Double slot! You won! (x4 to your bet)')
        print('Your winnings =>', bet)
        print()
        print('Your balance:', balance)
        print()
    else:
        lose += 1
        balance -= bet
        print(*numbers)
        print()
        print('You lose :(')
        print()
        print('Your balance:', balance)
        print()


    if balance == 0:
        print('Your balance = 0, you cant continue the game')
        print('Count of winnings:', win)
        print('Count of loses:', lose)
        break


    flag = True
    while True:
        cont = input('Would you like start next round? (y/n) => ').strip().lower()
        if cont == 'y':
            flag = True
            break
        elif cont == 'n':
            flag = False
            break
        else:
            print('You should input one char - y (yes) or n (no)')


    if flag == False:
        print()
        print('Thank you for the game! See you later')
        print('Count of winnings:', win)
        print('Count of loses:', lose)
        break
    else:
        continue

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

saif
25.05.22, 19:52

thanx a lot

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS