This is a working beta of a virtual slot machine I am working on. Eventually I'm going to add a GUI.

``````# Python 3
# Slot Machine
# By : RLS0812

# Note: The Odds are a bit off of what they actually are.

import random

def Reels():

# Winning Value, Fruit, Fruit, Fruit
Ret_Val = [0," "," "," "]

# Spin those reels !
Reel_Spin = random.randrange(1,4,1),random.randrange(1,4,1),random.randrange(1,4,1)

# Reel 1
if Reel_Spin == 1:
Ret_Val += 1
Ret_Val = "Banana"
if Reel_Spin == 2:
Ret_Val += 5
Ret_Val = "Orange"
if Reel_Spin == 3:
Ret_Val += 20
Ret_Val = "Cherry"

# Reel 2
if Reel_Spin == 1:
Ret_Val += 1
Ret_Val = "Banana"
if Reel_Spin == 2:
Ret_Val += 5
Ret_Val = "Orange"
if Reel_Spin == 3:
Ret_Val += 20
Ret_Val = "Cherry"

# Reel 3
if Reel_Spin == 1:
Ret_Val += 1
Ret_Val = "Banana"
if Reel_Spin == 2:
Ret_Val += 5
Ret_Val = "Orange"
if Reel_Spin == 3:
Ret_Val += 20
Ret_Val = "Cherry"

return Ret_Val

# Starting Values
Player_Money = 1000
Jack_Pot = 100
Run = True

while Run == True:

Bet = 0

# Reset if player goes broke
if Player_Money < 1:
input("You have no more money. Here is \$ 1000 \nPress Enter \n")
Player_Money = 1000
Jack_Pot = 100

# User input
Bet_Line = input(" Place Your Bet ! \n Jackpot \$ " + str(Jack_Pot) + "\n Money \$ " + str(Player_Money) + "\n Q = quit \n")
if Bet_Line == "q" or Bet_Line == "Q":
Run = False
break
try:
Bet = int(Bet_Line)
except:
print("Error: '" + Bet_Line + "' is not a number !")
pass

# Not enough money
if Bet > Player_Money:
print("Sory, you only have \$",Player_Money, "\n")

# Lets play !
elif Bet <= Player_Money and Bet != 0:
Jack_Pot += (int(Bet*.15))
Player_Money -= Bet
Is_Winner = Reels()
I_W = Is_Winner
Fruits = Is_Winner + " - " + Is_Winner + " - " + Is_Winner

# Match 3 - 1 in 9 chance
if I_W == 3 or I_W == 15 or I_W == 60:
print(Fruits + "\n" + "You Won \$ " + str(Bet*4) + " !!! \n")
Player_Money += (Bet*4)

# Jackpot 1 in 450 chance of winnning
JP1 = random.randrange(1,51,1)
JP2 = random.randrange(1,51,1)
if  JP1 == JP2:
print ("You Won The Jackpot !!!\nHere is your \$ " + str(Jack_Pot) + " prize! \n")
Jack_Pot = 100
elif JP1 != JP2:
print ("You did not win the Jackpot this time. \nPlease try again ! \n")

# Match 2 - 1 in 4.5 chance
elif I_W == 7 or I_W == 22 or I_W == 11 or I_W == 30 or I_W == 41 or I_W == 45:
print(Fruits + "\n" + "You Won \$ " + str(int(Bet*.5)) + " !!! \n")
Player_Money += (int(Bet*.5))

# No win
else:
print(Fruits + "\nPlease try again. \n")

#The End
print("- Program Terminated -")``````

## All 2 Replies

Was there a question there?

Some suggestions; use a for() loop to pick a random number on each pass, and another list/tuple for the values corresponding to the random number. Also, it is personal preference, but you can use a function to get the input and only return when the input is recognized. A subset of the code you posted:

``````import random

def Reels():

# Winning Value, Fruit, Fruit, Fruit
Ret_Val = [0," "," "," "]

values = ( (1, "Banana"), (5, "Orange"), (20, "Cherry") )

# Spin those reels !
for ctr in range(1, 4):  ## corresponds to element number in Ret_Val
Reel_Spin = random.randrange(0,3,1)  ## element number in values
this_wheel = values[Reel_Spin]
Ret_Val += this_wheel
Ret_Val[ctr] = this_wheel

return Ret_Val

def user_input(Jack_Pot, Player_Money):
""" User input
Return True and the bet amount, or False and zero to exit
"""
while 1:
print("\n Place Your Bet ! \n Jackpot \$%-9.2f" % (Jack_Pot))
print(" Money = \$%-9.2f " % (Player_Money))
print(" Q = quit ")
Bet_Line = input(" ")

if Bet_Line in ["q", "Q"]:
return False, 0

try:
Bet = int(Bet_Line)
return True, Bet
except:
print("Error: '" + Bet_Line + "' is not a number !")

Jack_Pot=2000
Player_Money=100

bet_placed, Bet = user_input(Jack_Pot, Player_Money)
while bet_placed:
# Not enough money
if Bet >= Player_Money:
print("Sorry, you only have \$%-d \n" % (Player_Money))
else:
print Reels()

bet_placed, Bet = user_input(Jack_Pot, Player_Money)  ## get next bet``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.