Posts: 5
Threads: 1
Joined: Oct 2017
import random
print("Welcome to my Online Roulette game!")
print("Your starting balance is $50, or 5000 tokens.")
## Sets starting balance to 5000
balance=5000
## Function that should occur when a win takes place on green
def greenWin():
balance=balance+greenwin
print("The roll was a " + str(roll) +".")
print("Your balance is now "+ str(balance)+" tokens. Congratulations!")
## Function that should occur when a win takes place on red or black
def redBlackWin():
balance=balance+redblackwin
print("The roll was a " + str(roll) +".")
print("Your balance is now "+ str(balance)+" tokens. Congratulations!")
## Function that should occur when any bet is lost
def loss():
print("The roll was a " + str(roll) +".")
print("Your balance is now "+ str(balance)+" tokens.")
## Game goes on until balance is 0, then the for loop breaks
for i in range(999999999999):
## Asks user how many tokens should be bet
betamount=int(input("How many tokens would you like to bet?"))
## Asks user what color to bet on
betcolor=int(input("Bet on red (enter 1), black (enter 2), or green (enter 3)?"))
## Winning on a green bet is 35 times the amount of money bet
greenwin=betamount*35
## Winning on a red or black bet is 2 times the amount of money bet
redblackwin=betamount*2
## Rolls a random number from 0 to 36
roll=random.randint(0,36)
## Lists winning numbers for red and black
red=[1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36]
black=[2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35]
## Subtracts what you bet from your balance
balance=balance-betamount
## If you bet on red:
if betcolor is 1:
if roll is red:
redBlackWin()
else:
loss()
## If you bet on black:
elif betcolor is 2:
if roll is black:
redBlackWin()
else:
loss()
## If you bet on green:
elif betcolor is 3:
if roll is 0:
greenWin()
else:
loss()
## If your balance is 0 for loop breaks and game ends
if balance is 0:
print("You have ran out of money. Better luck next time!")
break Here is my extremely simple code for a roulette game I've made using IDLE. I'm fairly new to Python and can't quite figure out why this isn't working. There is no error, but the problem is being able to win. Even if you do roll a red number while betting on red, it will subtract from your balance. Winning on green works, but black and red do not. Losing works just fine. As an example, say I bet 1000 tokens on 1 (or red). I roll a number from the black list, and lose 1000 tokens. I bet 1000 tokens on red again and this time roll a 7, which is a red number. I now have 3000 tokens instead of the 5000 tokens I should have. I feel like my code should work perfectly fine and don't really understand why it doesn't.
Posts: 591
Threads: 26
Joined: Sep 2016
Oct-19-2017, 08:10 AM
(This post was last modified: Oct-20-2017, 11:48 PM by Mekire.)
Quote:I feel like my code should work perfectly fine and don't really understand why it doesn't.
Not a good attitude. Your code has a lot of major flaws. You need to stop with all the global variables; they are making what should be very short and straight forward, a spaghetti mess.
Your main issue is you are writing if roll is red: when what you mean is if roll in red: .
This is also pretty funny for i in range(999999999999): . How about while True:
Posts: 5
Threads: 1
Joined: Oct 2017
Well thank you very much, I can tell you didn't see my line about being fairly new, but still thank you. I hadn't learned about the "in" part yet, or the "while True". While I appreciate your reply I was just stating I FEEL like it should work fine, and your attitude wasn't very good either. Even if you are the master at everything Python I clearly am not, so no need to be so critical and harsh.
Posts: 591
Threads: 26
Joined: Sep 2016
If you would like to continue improving this piece of code, post what you have with your fixes so far and we will work forward.
Or if you'd rather be offended that's fine too.
Posts: 5
Threads: 1
Joined: Oct 2017
import random
red=[1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36]
black=[2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35]
balance=5000
print("Welcome to Chad's Online Roulette game!")
print("Your starting balance is $50, or 5000 tokens.")
while True:
betamount=int(input("How many tokens would you like to bet?"))
betcolor=int(input("Bet on red (enter 1), black (enter 2), or green (enter 3)?"))
roll=random.randint(-1,36)
greenwin=betamount*35
redblackwin=betamount*2
balance=balance-betamount
if betcolor is 1:
if roll in red:
balance=balance+redblackwin
print("The roll was a " + str(roll) +".")
print("Your balance is now "+ str(balance)+" tokens.")
else:
print("The roll was a " + str(roll) +".")
print("Your balance is now "+ str(balance)+" tokens.")
elif betcolor is 2:
if roll in black:
balance=balance+redblackwin
print("The roll was a " + str(roll) +".")
print("Your balance is now "+ str(balance)+" tokens.")
else:
print("The roll was a " + str(roll) +".")
print("Your balance is now "+ str(balance)+" tokens.")
elif betcolor is 3:
if roll is 0 or roll is -1:
balance=balance+greenwin
print("The roll was a 0.")
print("Your balance is now "+ str(balance)+" tokens.")
else:
print("The roll was a " + str(roll) +".")
print("Your balance is now "+ str(balance)+" tokens.")
if balance is 0:
print("You have ran out of money. Better luck next time!")
break I fixed the if statements, and used the while True.
Posts: 4,220
Threads: 97
Joined: Sep 2016
Seems to work. I would note that there is no way for the player to quit the game. They can only stop playing (without ctrl-c) when they run out of money. The casino might like that, but I don't think the players will.
Posts: 5
Threads: 1
Joined: Oct 2017
Added in if betcolor is 4:
print("Your ending balance is "+ str(balance)+" tokens.")
break betcolor input now comes first, and if you enter 4 the program stops and tells you the balance you ended with.
Posts: 4,220
Threads: 97
Joined: Sep 2016
That's good, but I just noticed that you are using the 'is' operator for comparisons. You should only do that in certain cases, it doesn't check for two equal objects, it checks for the exact same object. For testing if two objects are equal, use == (double equals sign). It works here, but only in certain implementations of Python.
Posts: 591
Threads: 26
Joined: Sep 2016
Oct-20-2017, 11:49 PM
(This post was last modified: Oct-20-2017, 11:49 PM by Mekire.)
Here are a few ideas just to clean things up a bit.
You essentially print the same thing every run so instead of putting the prints in each conditional do it once at the end.
Also, the augmented assignment operators are great here.
Ex. instead of writing value = value + extra you can just write value += extra .
This feature is pretty common in many languages.
import random
red = [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36]
black = [2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35]
balance = 5000
print("Welcome to Chad's Online Roulette game!")
print("Your starting balance is $50, or 5000 tokens.")
while True:
betamount = int(input("How many tokens would you like to bet?"))
betcolor = int(input("Bet on red (enter 1), black (enter 2), or green (enter 3)?"))
roll = random.randint(-1,36)
greenwin = betamount*35
redblackwin = betamount*2
balance -= betamount
if betcolor == 1 and roll in red:
balance += redblackwin
elif betcolor == 2 and roll in black:
balance += redblackwin
elif betcolor == 3 and roll in (0, -1):
balance += greenwin
print("The roll was a {}.".format(roll))
print("Your balance is now {} tokens.".format(balance))
if balance == 0:
print("You have ran out of money. Better luck next time!")
break On Ichabod's point of not using is , the reason it is working here is because all your numbers are very small and python is optimized by keeping singleton instances of these. With bigger numbers however you would have no guarantee this would work.
>>> a = 67849
>>> b = 67849
>>> a is b
False
>>> a == b
True
>>> Another thing you could consider adding is input validation. Currently a player can bet more money than the actually have, or flat out enter invalid inputs.
Posts: 5
Threads: 1
Joined: Oct 2017
Oct-21-2017, 02:22 AM
(This post was last modified: Oct-21-2017, 02:23 AM by chadandersen1.)
The program now works as expected, and checks if the bet color is invalid, as well as checks if the bet amount is a negative number or more than the players balance. Thank you to both of you for your help, I learned quite a bit and without you I wouldn't have gotten the program to work.
import random
red = [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36]
black = [2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35]
validcolors = [1, 2, 3, 4]
balance=5000
print("Welcome to Chad's Online Roulette game!")
print("Your starting balance is $50, or 5000 tokens.")
while True:
betcolor = int(input("Bet on red (enter 1), black (enter 2), or green (enter 3). Enter 4 to stop playing. "))
if betcolor == 4:
print("Your ending balance is {} tokens.".format(balance))
while True:
if betcolor not in validcolors:
print("{} is not a valid color. Please enter a valid color.".format(betcolor))
betcolor = int(input("Bet on red (enter 1), black (enter 2), or green (enter 3). Enter 4 to stop playing. "))
if betcolor in validcolors:
break
betamount = int(input("How many tokens would you like to bet?"))
while True:
if betamount > balance or betamount < 0:
print("Your bet amount is invalid.")
betamount = int(input("How many tokens would you like to bet?"))
if betamount < balance or betamount == balance:
break
roll = random.randint(-1,36)
greenwin = betamount*35
redblackwin = betamount*2
balance -= betamount
if betcolor == 1 and roll in red:
balance += redblackwin
elif betcolor == 2 and roll in black:
balance += redblackwin
elif betcolor == 3 and roll in (0, -1):
balance += greenwin
print("The roll was a {}.".format(roll))
print("Your balance is now {} tokens.".format(balance))
if balance == 0:
print("You have ran out of money. Better luck next time!")
break
|