Casino game
SolvedHello,
I need help writing a Python program based on the following statement:
Consider a Casino: a roulette has 13 numbers (0 - 12):

The player has several types of bets available:
-
they can choose to bet on the winning number, in which case, if they win, they earn twelve times their stake;
-
they can choose to bet on the parity of the winning number (even or odd), in which case, if they win, they earn twice their stake;
-
finally, they can choose to bet on the color of the winning number (red or black), in which case, if they win, they also earn twice their stake.
If the player loses their bet, they do not get their stake back.
To simplify, we assume that the number 0 is neither red nor black, but is even. To simplify even further, we assume that the player always bets 10 euros.
Write a program that helps the dealer determine the amount the casino must give to the player.
The program will read, in order, two integers as input: the player's bet (represented by a number between 0 and 16, see description below), and the number drawn (a number between 0 and 12). The program will then display the amount won by the player.
Inputs for the player's bet:
-
number between 0 and 12: the player bets on the corresponding number
-
13: the player bets on even
-
14: the player bets on odd
-
15: the player bets on the color red
-
16: the player bets on the color black
Here is my code:
a = int(input()) b = int(input()) if b == a: print(10 * 12) elif a == 0 or (a == 13 and b % 2 == 0): print(10 * 2) elif a == 14 and b % 2 != 0: print(10 * 2) elif (a == 15 and b in [1, 3, 5, 7, 9, 11]) or (a == 16 and b in [2, 4, 6, 8, 10, 12]): print(10 * 2) else : print(10 * 0)
This code does not work for all pairs of numbers. Could you please help me improve this program?
Thank you very much.
6 answers
Hello, for which couple does it not work?
Here is the new code with the parentheses inserted:
a = int(input()) b = int(input()) if b == a: print(10 * 12) elif (a == 0) or (a == 13) or (a == 14) and (b % 2 == 0): print(10 * 2) elif (a == 15) or (a == 16) and (b % 2 == 0): print(10 * 2) else : print(10 * 0)
But it's not working. For example, entering the pair 13 and 1 should return 0 but it returns 20. I think it’s line 2 (Even and odd) that is causing the problem.
What do you think?
Thank you for the help
Hello,
Several recommendations:
- Rather than using a and b, you could use more descriptive variable names (for example bet and stake). It is also advisable to check whether the entered parameters are correct.
bet = int(input("Bet [0-16]?")) if not 0 <= bet <= 16: raise ValueError(f"Invalid bet: {bet}") draw = int(input("Draw [0-16]?")) if not 0 <= draw <= 16: raise ValueError(f"Invalid draw: {draw}") - To improve the readability of your program, you can define "constants" (in fact there are no constants in Python but that's the idea) that allow you to abstract special bets:
BET_EVEN = 13 BET_ODD = 14 BET_RED = 15 BET_BLACK = 16
... or in a more concise way:
(BET_EVEN, BET_ODD, BET_RED, BET_BLACK) = range(13, 17)
- You can write a function that determines whether a draw corresponds to a red square (respectively to a black square, respectively to an even number, respectively to an odd number) given a bet.
def is_even(draw): return draw % 2 == 0 def is_red(draw): return draw in {1, 3, 5, 7, 9, 12} # etc... - The tests applied will determine the multiplier for winnings (*2, *12, *0) depending on the nature of the bet and whether it is successful.
if bet == BET_RED and is_red(draw): multiplier = 2 elif bet == BET_EVEN and is_even(draw): multiplier = 2 ...
- You can factor the tests that lead to the same multiplier, but be careful because the logical AND operator takes precedence over the logical OR operator:
if ( (bet == BET_RED and is_red(draw)) or (bet == BET_EVEN and is_even(draw)) or ... ): multiplier = 2 elif ...: ... ...
- You can define a winnings variable that is calculated based on stake and multiplier.
Good luck
Hello @mamiemando StatusModerator
First of all, thank you for your very detailed and rich feedback. I note that you have a much more advanced level than me. Unfortunately, I don't understand everything.
I agree on the effect of directly naming the variables a and b, and I take note of that.
To be completely transparent, I am in a beginner MOOC on Learning to Code with Python. I'm discovering the material step by step, and for each exercise, I have to apply the concepts covered so far. That's why I mentioned that I don't understand all the details in your response, although I grasped some nuances, for example on the priority of logical operators.
Thus, I improved my code. But it still doesn't work. Can you please correct it using simple concepts?
a = int(input()) b = int(input()) if b == a: print(10 * 12) elif (a == 0 and b % 2 == 0) or (a == 14 and b % 2 == 0) or (a == 14 and b % 2 == 1): print(10 * 2) elif (a == 13) and b % 2 == 0: print(10 * 2) elif (a == 15) or (a == 16) and b % 2 == 0: print(10 * 2) else : print(10 * 0)
How can we express odd with modulo %, can we write: b % 2 == 1 to express ODD?
Thanks again for your help
- l1 to l5: ok
- l6: I suppose that a refers to the bet and that b refers to the drawing?
- The test seems wrong because b is necessarily even or odd, so the test simplifies to:
if (a == 0 and b % 2 == 0) or (a == 14)
... either "the player bets on 0 and draws an even number, or the player bets odd". In particular, if the player bets odd, they will enter this test regardless of the value of the drawing. I doubt this is what you want to write.
- The test seems wrong because b is necessarily even or odd, so the test simplifies to:
-
l8: the test means "the player bets even" and gets an even number, so that seems correct
-
l10: the test means "the player bets red, or he bets black and the drawing is even". This is also doubtful, because you enter this test as soon as the bet is red, no matter the drawing. Furthermore, the color and parity do not match, so the second part of the test also seems incorrect.
How to express odd parity with the modulo %, for odd can we write: b % 2 == 1 to express ODD?
Yes. The operator x % y returns the remainder of the Euclidean division x / y and is always between 0 and y - 1.
To write your program, I recommend making a test for each situation (the evens, the odds, the reds, the blacks) even if it means regrouping some tests later.
Good luck
@mamiemando StatusModerator
My code works persistently and especially your help has been very valuable. Really a big thank you for your kindness, help and patience, here is the code:
a = int(input()) b = int(input()) if b == a: print(10 * 12) elif (a == 14) and b % 2 == 1: print(10 * 2) elif (a == 13 and b % 2 == 0): print(10 * 2) elif (a == 15) and (b == 1 or b == 3 or b == 5 or b == 7 or b == 9 or b == 12): print(10 * 2) elif (a == 16) and (b == 2 or b == 4 or b == 8 or b == 11 or b == 10): print(10 * 2) else : print(10 * 0)
Indeed, it was necessary to compare each value. As it stands, I know that this code can still be improved and made more concise with the concepts mentioned earlier in your explanations. To this day, I am still not able to understand all these subtleties and I don't master all of that.
I really wanted to thank you for your demonstrated teaching ability here.
I would also like to ask you if you yourself were a computer developer or if you work in this field?
May I ask for your business card? (email?)
Thank you in advance.
Hello,
First of all, congratulations, the code now seems correct.
Some parentheses can be omitted (notably those on lines 6, 8 + those concerning the left operand of the and on lines 10 and 12).
Here’s what the program could look like once formatted according to the recommendations I made to you:
#!/usr/bin/env python3 PARI_PAIR = 13 PARI_IMPAIR = 14 PARI_ROUGE = 15 PARI_NOIR = 16 def est_pair(n: int) -> bool: return n % 2 == 0 def est_impair(n: int) -> bool: return n % 2 == 1 def est_rouge(n: int) -> bool: return n in {1, 3, 5, 7, 9, 12} def est_noir(n: int) -> bool: return n in {2, 4, 5, 6, 8, 10, 11} def saisie(): pari = int(input("Bet [0-16]?")) if not 0 <= pari <= 16: raise ValueError(f"Invalid bet: {pari}") tirage = int(input("Draw [0-12]?")) if not 0 <= tirage <= 12: raise ValueError(f"Invalid draw: {tirage}") return (pari, tirage) def calculer_multiplicateur(pari: int, tirage: int) -> int: if ( (pari == PARI_PAIR and est_pair(tirage)) or (pari == PARI_IMPAIR and est_impair(tirage)) or (pari == PARI_ROUGE and est_rouge(tirage)) or (pari == PARI_NOIR and est_noir(tirage)) ): return 2 elif pari == tirage: return 12 else: return 0 return 69 def main(): (pari, tirage) = saisie() multiplicateur = calculer_multiplicateur(pari, tirage) mise = 10 gains = multiplicateur * mise print(f"Bet: {pari} draw: {tirage} --> Winnings = {multiplicateur} * {mise}€ = {gains}€") def test(): for pari in range(0, 17): for tirage in range(0, 13): multiplicateur = calculer_multiplicateur(pari, tirage) print(f"Bet: {pari}, draw: {tirage} --> multiplier: {multiplicateur}") main() I really wanted to thank you for your demonstrated pedagogy here.
Thank you, it's always nice to receive compliments and some of the pedagogy also goes to yg_be.
I would also like to ask if you are a computer developer or do you work in this field?
Yes, I work in the field.
May I ask for your business card? (email?)
Forgive me, but I never share personal information through the forum. However, you can message me privately or ask other questions on the forum if needed.
I wish you a great continuation and good learning of Python ;-)
Do you know the order of operations for logical operators?