0
import random
money=int(0) # money starts at 0 always
bet=int(input("enter a bet.")) # user inputs a bet.
winningnumber=(random.randint(0,30)) # chooses random integer from 0 - 30, stores it as variable winningnumber
number = int(input("Pick a number 0-30"))
if number % 2 == 0: #checks if number is divisible by 2
 print("The number was even, you get 2x your money.") #prints the users winnings
 money = float(bet) * float(2) # multiplies the bet by 2
else:
 money = bet+0 # doesnt add anyuthing to the bet
if number % 10 ==0: #checks if number is divisible by 10
 print("The number was a multiple of 10, you get 3x your money.") #prints the users winnings
 money = bet*3 # multiplies the bet by 3
else:
 money = bet+0 # doesnt add anyuthing to the bet
primenum = ["3","5","7","11","13","17","19","23","29"] #list of prime numbers
if number == primenum ==0: #if number chosen by user = one of the numbers on the list, the user wins.
 print("The number was a prime number, you get 5x your money.") #prints the users winnings
 money = bet*5 # multiplies the bet by 5
else:
 money=bet+0 # doesnt add anything to the bet
print("the winning number was",winningnumber) #shows the player the winning number
print("Your money for the end of the round is",money) #prints money at end of round

Why doesn't this code work? I have tried multiple different ways yet the bet doesn't seem to multiply. I was expecting the code number to be multiplied at the end.

mkrieger1
24.2k7 gold badges68 silver badges84 bronze badges
asked Nov 6, 2022 at 23:25
0

2 Answers 2

1

You have three different if-else blocks. All three do different things to money if their respective conditions evaluate to True. However, if the last if-else block evaluates False, then regardless of what happened in the previous if-else blocks, the code in the final else block will run. That code sets money back to the original bet.

You can see this happening if you introduce print (money) after each if-else block and re-run your code.

Also - it's not clear to me how number == primenum == 0 would ever evaluate to True. primenum is a list of strings. number is an int. Therefore, this condition will never evaluate to True.

If what you're trying to do is check whether number is prime, then I would (first), define primenum as a list of integers, like so:

primenum = [3,5,7,11,13,17,19,23,29] #list of prime numbers

and then, change the criterion in your last if-else block to:

if number in primenum:

One way to make your if-else blocks work would be to use if-elif-else, like so:

import random
money=int(0) # money starts at 0 always
bet=int(input("enter a bet.")) # user inputs a bet.
winningnumber=(random.randint(0,30)) # chooses random integer from 0 - 30, stores it as variable winningnumber
primenum = [3,5,7,11,13,17,19,23,29] #list of prime numbers
number = int(input("Pick a number 0-30"))
if number % 10 == 0: 
 print("The number was a multiple of 10, you get 3x your money.") #prints the users winnings
 money = float(bet) * float(2) 
elif number % 2 == 0: 
 print("The number was even, you get 2x your money.") #prints the users winnings
 money = bet*3 
if number in primenum: #if number chosen by user = one of the numbers on the list, the user wins.
 print("The number was a prime number, you get 5x your money.") #prints the users winnings
 money = bet*5 # multiplies the bet by 5
else:
 money=bet+0 # doesnt add anything to the bet
print("the winning number was",winningnumber) #shows the player the winning number
print("Your money for the end of the round is",money) #prints money at end of round

Bear in mind that "winningnumber" hasn't played a role in the program at all - except to be generated at the start, and printed to screen at the end. The above may not reflect your original intent, it's just ONE way in which the control-flow could be made to work so you reliably modify money when any of the three conditions evaluates to True.

answered Nov 6, 2022 at 23:48
Sign up to request clarification or add additional context in comments.

Comments

0

The problem with your code relates to the multiple if-else blocks. You have a problem with your prime number section, which will always equate to False, and as such you will set money=bet+0 - which will override any other successful changes made to money.

Your winning number does not seem to have any impact on the flow of code, so as long as you choose an even number or a number divisible by 10, it should multiply. But then when you get to the section to test if it is a prime number, it will ALWAYS equate to FALSE, and as mentioned before will set money=bet+0 - and undo anything done prior.

You need to make sure that you set money = money + (bet + 0) This way your cumulative effects are maintained. You have to do this for the other money calculations as well.

And you need to fix the prime number section:

primenum = [3,5,7,11,13,17,19,23,29]
if number in primenum:
...

Here is the complete code based on what I think you were trying to do:

import random
money=0 # money starts at 0 always
bet=int(input("enter a bet.")) # user inputs a bet.
winningnumber=(random.randint(0,30)) # chooses random integer from 0 - 30, stores it as variable winningnumber
number = int(input("Pick a number 0-30"))
primenum = [3,5,7,11,13,17,19,23,29] #list of prime numbers
if winningnumber == number:
 if number % 2 == 0: #checks if number is divisible by 2
 print("The number was even, you get 2x your money.") #prints the users winnings
 money = money + (bet * 2) # multiplies the bet by 2
 else:
 money = money + (bet + 0) # doesn't add anything to the bet
 if number % 10 == 0: #checks if number is divisible by 10
 print("The number was a multiple of 10, you get 3x your money.") #prints the users winnings
 money = money + (bet * 3) # multiplies the bet by 3
 else:
 money = money + (bet + 0) # doesn't add anything to the bet
 if number in primenum: #if number chosen by user = one of the numbers on the list, the user wins.
 print("The number was a prime number, you get 5x your money.") #prints the users winnings
 money = money + (bet * 5) # multiplies the bet by 5
 else:
 money = money + (bet + 0) # doesn't add anything to the bet
print("the winning number was",winningnumber) #shows the player the winning number
print("Your money for the end of the round is",money) #prints money at end of round
answered Nov 7, 2022 at 0:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.