Sean.D
Sean.D

Reputation: 97

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'

Upvotes: 0

Views: 45

Answers (2)

dmlicht
dmlicht

Reputation: 2438

You are never changing the value of your variable continued.

When the user attempts to bet more money than they have you execute:

if bet>money:
  ...
  continued=='z'

This will just evaluate to false, not change the state of continued to "z". It seems like what you intended to write was:

continued = 'z'

You did the same thing again later in your code when the user runs out of money:

if moneyleft<=0:
  ...
  continued=='z'

In addition, it would make your code more clear if you change the name of "continued" to "continue" and you used a boolean instead of a character. It's not clear what 'g' and 'z' stand for. If continue is either true or false, its pretty clear that you mean, it's true you should continue the program, or its false, you should not continue the program. Good luck with your game :)

Upvotes: 0

user94559
user94559

Reputation: 60133

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

Upvotes: 1

Related Questions