• Steam recently changed the default privacy settings for all users. This may impact tracking. Ensure your profile has the correct settings by following the guide on our forums.

Roulette program in Python (newbie)

Andy

Champion of the Sun
I'm relatively new at programming, and Python is my first language so please take that into consideration before you read on.

Right, I am trying to create a Roulette type of game in Python. It asks for a number between 0 and 36, or it asks for you to place a bet on it being an odd or even number. This is the bet the user is making. It then generates a random number between 0 and 36.

What I need to do, is for the program to check if the bet is an integer or not, and if it is, see if it matches the random number that was made. If it does, you have won and it should say "Big Winner". If it isn't right, then it should display something like "Sorry, you have won nothing".

BUT, if the user inputted "odd" as their bet, it should check to see if it is an odd or even number. If it's odd, it should give them the message "Little Win". (So if they guess if the random number generate is odd or even correctly, they win).

However, I'm stuck on how to go about doing this. As I have said, I am quite new to programming, and have only been able to do the following code:
[HIGHLIGHT=python]
import random


randomnumber = random.randrange(0,36)
print randomnumber

bet = raw_input("Please enter either a number between 0 and 36, or choose 'odd' or 'even'")

def roulette():
odd = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35]
even = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34]
if int(bet) == randomnumber:
print "Big Winner"
elif int(bet) != randomnumber:
print "Unlucky, you have won nothing"
if randomnumber == odd:
if bet == "odd":
print "Congrats"

roulette()[/HIGHLIGHT]

That's how far I have gotten, but I know it's wrong. Should I create another function or something? Thanks in advance to anyone who helps =)
 

EvilSeph

Administrator
I think you need to clarify the requirements of this program as the information you're providing does not exactly match with the code you've attempted to write.
 

Andy

Champion of the Sun
Yeah, I got a few bits wrong in the post, sorted now.

Anyway, with some help from Moca, Sousanator and yourself I have gotten it working as I wished now :)

[highlight=python]
import random


randomnumber = random.randrange(0,36)
print randomnumber

bet = raw_input("Please enter either a number between 1 and 36, or choose 'odd' or 'even'")

def roulette():
if bet.isdigit():
if int(bet) == randomnumber:
print "Big Winner"
elif int(bet) != randomnumber:
print "Unlucky, you have won nothing"
if bet == "even":
if randomnumber%2 == 0:
print "Little Win"
elif randomnumber%2 != 0:
print "Sorry, you have won nothing"
if bet == "odd":
if randomnumber%2 != 0:
print "Little Win"
elif randomnumber%2 ==0:
print "Sorry, you have won nothing"

roulette()
[/highlight]

Still messy, but this has been a good learning experience =) If anyone wants to help me improve this by telling me if I have done unnecessary things etc, feel free.
 

EvilSeph

Administrator
[highlight=python;4]randomnumber = random.randrange(0,36)[/highlight]

Here you're generating a number between 0 and 36, inclusive (if I'm not mistaken) and this is not what you want (at least, from what I can see - hence why I asked you to clarify the requirements of your program) as there will be a chance that the user will be wrong regardless of their guess, because the random number generated is 0.

[highlight=python;7]bet = raw_input("Please enter either a number between 1 and 36, or choose 'odd' or 'even'")[/highlight]

Here you prompt the user to enter a number between 1 and 36...the problem is, there is some ambiguity here that could lead to some difficulty using your program:

Does your program expect a number between 1 and 36 (2-35) or a number between 0 and 36 (1-35) [both non-inclusive]? It's all very confusing and something that will result in unexpected usage of your program...

Furthermore, I'm glad you're employing the use of raw_input instead of input, as the latter is passed to eval() before it is given to your program, thus resulting in some security risks if your user is crafty, because:

  • If a user enters a number, say, 9, it'll be eval'ed and converted to an integer.
  • If a user enters a string, it won't be a string unless they quote the string.
  • But worse, still, if a user is crafty, they'll recognise this problem and throw a system command at it - and it will execute.

Whereas raw_input returns everything the user inputs as a string, which is generally better for input validation anyway.

Despite this being a small program, I ask that you get into the habit of commenting things...especially since you're learning.

Whenever asking a user to enter something within a range, you're going to have to check if their input is within the range you're looking for. If this isn't the case, re-prompt them to input a number after having displayed an error. What if I enter the number 40? Mispelt odd or even?

I noticed you've used multiple if statements when this really isn't necessary. Instead, you should be employing the use of elifs and get into the habit of using proper decision structures.

Otherwise, things look alright and I'm glad that you're learning from us :)
 

Moca

New Member
Yeah, I got a few bits wrong in the post, sorted now.

Anyway, with some help from Moca, Sousanator and yourself I have gotten it working as I wished now :)

[highlight=python]
import random


randomnumber = random.randrange(0,36)
print randomnumber

bet = raw_input("Please enter either a number between 1 and 36, or choose 'odd' or 'even'")

def roulette():
if bet.isdigit():
if int(bet) == randomnumber:
print "Big Winner"
elif int(bet) != randomnumber:
print "Unlucky, you have won nothing"
if bet == "even":
if randomnumber%2 == 0:
print "Little Win"
elif randomnumber%2 != 0:
print "Sorry, you have won nothing"
if bet == "odd":
if randomnumber%2 != 0:
print "Little Win"
elif randomnumber%2 ==0:
print "Sorry, you have won nothing"

roulette()
[/highlight]

Still messy, but this has been a good learning experience =) If anyone wants to help me improve this by telling me if I have done unnecessary things etc, feel free.

As EvilSeph stated already, the way that you check for the value of 'bet' isn't correct. You are checking the value of bet three times when you want to check it only once. Although your code works in this case, there are times when the conditions after the first true statement may also be true and can cause serious problems in your program.

That doesn't mean that you shouldn't have if statements like that, but it isn't what you intended this time. In the end, it's up to you as the programmer to figure out how you want the program to flow and to be careful to not make mistakes like this.

If you have anymore questions, then feel free to ask :)
 

Andy

Champion of the Sun
Thanks guys for the help, it's very appreciated :)

I have changed the two "if" statements to "elif", and it's funny that you should mention about commenting, as just after I posted it I put some comments on it :p

And yeah, I think my teacher was unclear about the whole 1-36 business. After school today I am going to fix it up and make it cleaner, and possible add some other stuff.
 

Andy

Champion of the Sun
Well I have quickly made some adjustments, including adding an error if anything incorrect is added. The one thing I have not been able to do is make the program return to raw_input when they input something wrong. Instead I have just made it exit the program. Anyone know how to do this?

[highlight=python]# note, for the "odd" and "even" parts to work, the input by the user MUST be case sensitive and exact by the letter("odd" and not "odd number")

import random
import sys

randomnumber = random.randrange(1,35) #generates a random number that is between 0 and 36
print randomnumber #This is just for testing purposes, this would not be included.

bet = raw_input("Please enter either a number between 0 and 36, or choose 'odd' or 'even'")


if bet.isdigit():
if int(bet) > 35: #If the digit is greater than 35, it will print an error and exit the program
print "Sorry, you have entered an incorrect value"
sys.exit()
elif int(bet) < 1: #If the digit is less than 0, it will print an error and exit the program
print "Sorry, you have entered an incorrect value"
sys.exit()


def roulette(): #defines the function "roulette"
if bet.isdigit(): # if the value the user inputted is a number, then: (a way of determining if its a number or a string)
if int(bet) == randomnumber:
print "Big Winner"
elif int(bet) != randomnumber:
print "Unlucky, you have not won"
elif bet == "even":
if randomnumber%2 == 0: # if the remainder is equal to 0, then it is an even number
print "Little Win"
elif randomnumber%2 != 0: # if the remainder is not equal to 0, then it is an odd number
print "Sorry, you have not won"
elif bet == "odd":
if randomnumber%2 != 0:
print "Little Win"
elif randomnumber%2 ==0:
print "Sorry, you have not won"
elif bet != "odd" or "even": #If a string other than "odd" or "even" is entered, it will exit the program
print "Sorry, you have entered an incorrect value."
sys.exit()


roulette()
[/highlight]
 

EvilSeph

Administrator
[highlight=python]# note, for the "odd" and "even" parts to work, the input by the user MUST be case sensitive and exact by the letter("odd" and not "odd number")[/highlight]

This is an interesting comment. If you acknowledge the problem, why don't you just fix it? The logic to solve this problem is simple:
1. Determine what kind of input is required by your program (in this case, the case of the user's input doesn't matter, only the spelling does)
2. Based on this, develop a means of formatting the user's input to your liking (i.e. take the user's input and make it what you're looking for, without completely altering it), before passing it to your program. For this program we can just lowercase() all the user's input, like so:
[highlight=python;1;10]# note, for the "odd" and "even" parts to work, the input by the user MUST be case sensitive and exact by the letter("odd" and not "odd number")

import random
import sys

randomnumber = random.randrange(1,35) #generates a random number that is between 0 and 36
print randomnumber #This is just for testing purposes, this would not be included.

bet = raw_input("Please enter either a number between 0 and 36, or choose 'odd' or 'even'")
bet = bet.lowercase() # Convert user's input to lowercase to ease input validation.

if bet.isdigit():
if int(bet) > 35: #If the digit is greater than 35, it will print an error and exit the program
print "Sorry, you have entered an incorrect value"
sys.exit()
elif int(bet) < 1: #If the digit is less than 0, it will print an error and exit the program
print "Sorry, you have entered an incorrect value"
sys.exit()


def roulette(): #defines the function "roulette"
if bet.isdigit(): # if the value the user inputted is a number, then: (a way of determining if its a number or a string)
if int(bet) == randomnumber:
print "Big Winner"
elif int(bet) != randomnumber:
print "Unlucky, you have not won"
elif bet == "even":
if randomnumber%2 == 0: # if the remainder is equal to 0, then it is an even number
print "Little Win"
elif randomnumber%2 != 0: # if the remainder is not equal to 0, then it is an odd number
print "Sorry, you have not won"
elif bet == "odd":
if randomnumber%2 != 0:
print "Little Win"
elif randomnumber%2 ==0:
print "Sorry, you have not won"
elif bet != "odd" or "even": #If a string other than "odd" or "even" is entered, it will exit the program
print "Sorry, you have entered an incorrect value."
sys.exit()


roulette()[/highlight]

However, this isn't enough as the user could leave a space or two in their input by mistake, invalidating their input. So, let's strip all whitespace from their input using strip():

[highlight=python;1;10,11]# note, for the "odd" and "even" parts to work, the input by the user MUST be case sensitive and exact by the letter("odd" and not "odd number")

import random
import sys

randomnumber = random.randrange(1,35) #generates a random number that is between 0 and 36
print randomnumber #This is just for testing purposes, this would not be included.

bet = raw_input("Please enter either a number between 0 and 36, or choose 'odd' or 'even'")
bet = bet.lowercase().strip() # Convert user's input to lowercase to ease input validation.
# Strip all whitespaces to ensure the user doesn't leave any in their input

if bet.isdigit():
if int(bet) > 35: #If the digit is greater than 35, it will print an error and exit the program
print "Sorry, you have entered an incorrect value"
sys.exit()
elif int(bet) < 1: #If the digit is less than 0, it will print an error and exit the program
print "Sorry, you have entered an incorrect value"
sys.exit()


def roulette(): #defines the function "roulette"
if bet.isdigit(): # if the value the user inputted is a number, then: (a way of determining if its a number or a string)
if int(bet) == randomnumber:
print "Big Winner"
elif int(bet) != randomnumber:
print "Unlucky, you have not won"
elif bet == "even":
if randomnumber%2 == 0: # if the remainder is equal to 0, then it is an even number
print "Little Win"
elif randomnumber%2 != 0: # if the remainder is not equal to 0, then it is an odd number
print "Sorry, you have not won"
elif bet == "odd":
if randomnumber%2 != 0:
print "Little Win"
elif randomnumber%2 ==0:
print "Sorry, you have not won"
elif bet != "odd" or "even": #If a string other than "odd" or "even" is entered, it will exit the program
print "Sorry, you have entered an incorrect value."
sys.exit()


roulette()[/highlight]

While this will work for your particular program because whitespace and casing isn't integral to its operation, I would like to make it clear that this isn't a one size fits all deal - you likely need to develop a solution for every input validation problem you come across.

If you can place yourself in the user's shoes, you'll find it is quite easy to do so.

A little note on your instructions:
[highlight=python;9]bet = raw_input("Please enter either a number between 0 and 36, or choose 'odd' or 'even'")[/highlight]

You're not keeping your instruction wording familiar to the user, and so, run the risk of confusing them. What does it mean to "choose" 'odd' or 'even'? For most people, they'd try clicking on them.

Regarding this:
[highlight=python;13]if bet.isdigit():
if int(bet) > 35: #If the digit is greater than 35, it will print an error and exit the program
print "Sorry, you have entered an incorrect value"
sys.exit()
elif int(bet) < 1: #If the digit is less than 0, it will print an error and exit the program
print "Sorry, you have entered an incorrect value"
sys.exit()[/highlight]

You can use "and" to have everything in one if statement (only because none of it needs to have an exclusive check in your program) - whether or not you want to do this, is up to you and your style of programming:
[highlight=python;13]if bet.isdigit() and int(bet) > 35 and int(bet) < 1:
print "Sorry, you have entered an incorrect value"
sys.exit()[/highlight]

However, since you're using exclusive checks, why not provide the user with better instructions based on what check fails?

[highlight=python;13;3,6]if bet.isdigit():
if int(bet) > 35: #If the digit is greater than 35, it will print an error and exit the program
print "Sorry, please enter a number less than 36."
sys.exit()
elif int(bet) < 1: #If the digit is less than 0, it will print an error and exit the program
print "Sorry, please enter a number greater than 0."
sys.exit()[/highlight]

In general, you should try making your program as user-friendly and idiot-proof as possible, without sacrificing things (again, based on your style/preference).

To answer your question, read through this thread on starting to pick up turing, as I have outlined the logic you should follow in it.

If you don't understand anything, or would like it to be translated to Python or whatever, feel free to ask. :)
 

A_Nub

Developer
Another good note would be to take into thought would be the string "odd number" as input. All the above programs would falsify that as input even though it is correct. So you need a way to check only if the string contains "odd" not if it equals odd.

to do so
bet = bet.lowercase()
if(bet.find("odd") > 0) returns true if odd is in the string bet
 
Top