Sean.D Sean.D - 4 months ago 9
Python Question

while loop not working-can't find similar code like mine that is not breaking with input

I am creating this game in python and I am trying to get the program to stop running if the user puts in a bet more than they have or if they run out of money. I have used while loops in the past but I can not remember how I did it and other while loop questions; I have tried their solutions but no prevail. If I can get this loop, then I think I can get the second one I will need so the user can keep playing and not have to enter a new starting value sense he/she will be literally playing 1 round only. Anyways, here is my code.

import time
import math
import random
continued='h'
continued2='g'
print("Welcome to gambling 50/50 shot game!!!"
"You will put in an amount to start with and type in"
"the amount you want to bet until your money you started with is gone."
"You will choose either black or red. "
"May the odds be in your favor.")
time.sleep(5)
while continued=='h':
moneyleft=int(input("How much money are you going to start with? $:"))
time.sleep(2)
bet=int(input("How much money are you going to bet this round? $:"))
if bet>moneyleft:
print("sorry, you can't bet more then what you have!")
time.sleep(2)
print("Sorry, I do not play with cheaters!!!")
continued=='z'
time.sleep(2)
colorchosen=int(input("Type 1, for black and 2 for red."))
time.sleep(2)
result=random.randint(1,2)
if result==1:
print("The color was black!!!")
if result==2:
print("The color was red!!!")
time.sleep(3)
if colorchosen==result:
moneyleft=moneyleft+bet
print("Congratulations!!! You won! Your money total is now $", moneyleft)
time.sleep(3)
if not colorchosen==result:
moneyleft=moneyleft-bet
print("Sorry, you lost. Your money total is now $", moneyleft)
time.sleep(3)
if moneyleft<=0:
print("Sorry, your all out of money!!!")
continued=='z'

Answer

Fixed a few things up. Notably, I did what commenters already suggested: I moved the initial money_left= input outside of the loop and I used break statements to exit the loop.

import time
import random

print("Welcome to gambling 50/50 shot game!!! "
      "You will put in an amount to start with and type in "
      "the amount you want to bet until your money you started with is gone. "
      "You will choose either black or red. "
      "May the odds be in your favor.")

time.sleep(5)
money_left = int(input("How much money are you going to start with? $"))

while True:
    time.sleep(2)
    bet = int(input("How much money are you going to bet this round? $"))

    if bet > money_left:
        print("Sorry, you can't bet more then what you have!")
        time.sleep(2)
        print("Sorry, I do not play with cheaters!!!")
        break

    time.sleep(2)

    color_chosen=int(input("Type 1 for black and 2 for red: "))
    time.sleep(2)

    result = random.randint(1,2)
    if result == 1:
        print("The color was black!!!")
    if result == 2:
        print("The color was red!!!")
    time.sleep(3)

    if color_chosen == result:
        money_left += bet
        print("Congratulations!!! You won! Your money total is now ${}.".format(money_left))
        time.sleep(3)
    else:
        money_left -= bet
        print("Sorry, you lost. Your money total is now ${}.".format(money_left))
        time.sleep(3)

        if money_left <= 0:
            print("Sorry, your all out of money!!!")
            break