Python Forum
Trouble with simple roulette game
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with simple roulette game
#9
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.
Reply


Messages In This Thread
RE: Trouble with simple roulette game - by Mekire - Oct-19-2017, 08:10 AM
RE: Trouble with simple roulette game - by Mekire - Oct-20-2017, 12:15 AM
RE: Trouble with simple roulette game - by Mekire - Oct-20-2017, 11:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple cards game blackpanda 3 4,385 Apr-10-2020, 08:46 PM
Last Post: TomToad
  Need help with simple guessing game! ghost0fkarma 2 2,933 Nov-03-2018, 01:19 AM
Last Post: ghost0fkarma
  Easy equipment system for simple game and problems with it naisyrk 3 3,443 Sep-01-2018, 10:05 AM
Last Post: Gribouillis
  Errors in simple text adventure game? ecloev 5 5,170 Apr-10-2018, 05:55 PM
Last Post: nilamo
  Simple Hangman Game Issue andrew95 2 4,517 Apr-02-2018, 02:24 PM
Last Post: andrew95

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020