Python Forum
Trouble with simple roulette game
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with simple roulette game
#1
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.
Reply
#2
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:
Reply
#3
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.
Reply
#4
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.
Reply
#5
I fixed the if statements, and used the while True.
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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.
Reply
#8
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#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
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple cards game blackpanda 3 4,268 Apr-10-2020, 08:46 PM
Last Post: TomToad
  Need help with simple guessing game! ghost0fkarma 2 2,821 Nov-03-2018, 01:19 AM
Last Post: ghost0fkarma
  Easy equipment system for simple game and problems with it naisyrk 3 3,319 Sep-01-2018, 10:05 AM
Last Post: Gribouillis
  Errors in simple text adventure game? ecloev 5 4,880 Apr-10-2018, 05:55 PM
Last Post: nilamo
  Simple Hangman Game Issue andrew95 2 4,364 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