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
This feature is pretty common in many languages.
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!") breakOn 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.