I am new to lerning Python and am struggling with creating this dice roll gambling game. I have spent a good week on it, and am still just frustrated with not being able to get it to work properly/fully. Basically I am having a hard tim egetting it to print properly and continue to ask for the user's dice roll guess (between 2 to 12). I thank anyone in advance for their help and suggestions!
-
You could put it all into a loop and [`break` (keyword to escape loops)](https://docs.python.org/3.8/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops) on the exit conditions! – ti7 Oct 31 '20 at 00:51
-
You forgot a few 'f''s in front of your strings that use variables. – RnRoger Oct 31 '20 at 01:17
3 Answers
You're total_bank()
method may be causing the trouble.
def total_bank():
bank = 500
while bank > 0:
print("You have ${bank} in your bank.")
bet = int(input("Enter your bet: "))
When you call this function, it'll tell you how much money you have in your bank, and once you enter in your bet, it'll do it again because bank is still >0. So, you'll just be stuck in this infinite loop.
ALSO, you'll need a lot of global
keywords in this case. Although it is not advised to use it, I think it's fine in this case. You're not actually changing the bank amount - that bank
variable is a local variable, meaning you can't access it outside the total_bank
function. You'll have to return bank
every time you call it.
So, the code should be
def total_bank():
bank = 500
print(f"You have ${bank} in your bank.")
# you forgot to make in a f-string
bet = int(input("Enter your bet: "))
bank-=bet
return bank
However, it may not be right for what your purposes are. For example, you can look limit the bet, etc. I'm just simplifying it.
Something like this is what you REALLY might want, but I'm not sure.
def total_bank():
bank = 500
print(f"You have ${bank} in your bank.")
# you forgot to make in a f-string
while bet <= 500: #whatever number is fine
bet = int(input("Enter your bet: "))
return bank, bet
bank, bet = total_bank()
Hope this answers your question!

- 359
- 2
- 11
If you make some changes to the total_bank
function and the main loop, you will get the results you want.
Try this code:
import random
def rollDice():
die1 = random.randint(1,6)
die2 = random.randint(1,6)
x = int(die1 + die2)
print('Roll', x)
return x
def prog_info():
print("My Dice Game .v02")
print("You have three rolls of the dice to match a number you select.")
print("Good Luck!!")
print("---------------------------------------------------------------")
print(f'You will win 2 times your wager if you guess on the 1st roll.')
print(f'You will win 1 1/2 times your wager if you guess on the 2nd roll.')
print(f'You can win your wager if you guess on the 3rd roll.')
print("---------------------------------------------------------------")
def total_bank(bank):
bet = 0
while bet <= 0 or bet > min([500,bank]):
print(f"You have ${bank} in your bank.")
a = input("Enter your bet (or Q to quit): ")
if a == 'q': exit()
bet = int(a)
return bank,bet
def get_guess():
guess = 0
while (guess < 2 or guess > 12):
try:
guess = int(input("Choose a number between 2 and 12: "))
except ValueError:
guess = 0
return guess
prog_info()
bank = 500
while True:
bank,bet = total_bank(bank)
guess = get_guess()
if guess == rollDice():
bank += bet
elif guess == rollDice():
bank += bet * .5
elif guess == rollDice():
bank = bank
else:
bank = bank - bet
print(f'You have ${bank} in your bank.')
print(f'Thanks for playing!\n')

- 11,175
- 2
- 7
- 15
'F' Strings
F-strings replace whatever is in the squiggly brackets {} with the value of an expression such as a return value from a function or just any variable/value:
print(f"abc{1+2}") # output: abc3
In your prog_info function, your print statements don't actually need F-Strings however, lines such as these need to use F-Strings since you need to substitute values in:
print("You have ${bank} in your bank.")
print('Die Roll #1 was {roll}')
Game Loop
You could implement a while True loop with and if statement inside to exit if any of your conditions are not met
bank = 500 # Initial Amount
while True:
# Ask user for their bet/guess
print(f'You have ${bank} in your account')
print(f'Choose a number between 2-12')
guess = get_guess()
if guess == 0:
break # This stops the loop
# Roll Dice, Add/Subtract from their bank account, etc.
# Check if their bank account is above 0 after potentially subtracting from their account
if bank <= 0:
break
else:
print("Thanks for playing") # Game has ended

- 56
- 4