Ad

Python Program Ends Even If The Condition Are Not Met

- 1 answer

The condition is that the program will end: -if the balance is less than or equal to zero. -if the balance is greather than or equal to 200.

But the problem is that it ends after I input 1 or 2 (1 for heads and 2 for tails) and you need to run it again and the balance is not saved.

Here is my code:

import random
 
def Guess_Check(guess,balance): #function for guess check
    coin_flip = int( random.choice([1,2]))
    if coin_flip == 1:
        print("It's heads!!")
    else:
        print("It's tail!")
    if guess == coin_flip: #if guess is correct
        print("Congrats you guessed right, You won $9.")
        balance = balance+9
    else: #if guess is wrong
        print("Sorry your guess was wrong, You loss $10.")
        balance = balance-10
    print("Avalilable balanace is :", balance)
    return (balance)
 
def Balance_Check(Balance): #Balance Check
    if Balance <= 10: #we can't play the game if balance is below $10
        print("Sorry!! You run out of money.")
    return(1)
    if Balance >=200:
        print("Congrats!! You reached your Target $200.")
    return(1)
 
balance = 100 #beginning amount
while True:
    bal = Balance_Check(balance) #check the available balance
    if bal==1:
        break
print("Guess heads by entering 1 or tails by entering 2 for this coin flip.") #Asking the player to guess
guess = int(input())
Balance = Guess_Check(guess,balance)
Ad

Answer

You messed up a few of the things like which balance to take at the right time when to stop the loop etc.

Here's the code if you want to take the user input once and then check the guess:

import random

def Guess_Check(guess,balance): #function for guess check
    coin_flip = random.choice([1,2])
    if coin_flip == 1:
        print("It's heads!!")
    else:
        print("It's tail!")
    if guess == coin_flip: #if guess is correct
        print("Congrats you guessed right, You won $9.")
        balance = balance+9
    else: #if guess is wrong
        print("Sorry your guess was wrong, You loss $10.")
        balance = balance-10
    print("Avalilable balanace is :", balance)
    return (balance)

def Balance_Check(Balance): #Balance Check
    if Balance <= 10: #we can't play the game if balance is below $10
        print("Sorry!! You run out of money.")
        return 0
    elif Balance >=200:
        print("Congrats!! You reached your Target $200.")
        return 1

balance = 100 #beginning amount

print("Guess heads by entering 1 or tails by entering 2 for this coin flip.") #Asking the player to guess
guess = int(input())
while Balance_Check(balance) not in [0,1]: # You need to run the loop till balance becomes 0 or 200
    balance = Guess_Check(guess,balance)

But if you want to take user input on every chance use:

import random

def Guess_Check(guess,balance): #function for guess check
    coin_flip = random.choice([1,2])
    if coin_flip == 1:
        print("It's heads!!")
    else:
        print("It's tail!")
    if guess == coin_flip: #if guess is correct
        print("Congrats you guessed right, You won $9.")
        balance = balance+9
    else: #if guess is wrong
        print("Sorry your guess was wrong, You loss $10.")
        balance = balance-10
    print("Avalilable balanace is :", balance)
    return (balance)

def Balance_Check(Balance): #Balance Check
    if Balance <= 10: #we can't play the game if balance is below $10
        print("Sorry!! You run out of money.")
        return 0
    elif Balance >=200:
        print("Congrats!! You reached your Target $200.")
        return 1

balance = 100 #beginning amount


while Balance_Check(balance) not in [0,1]: # You need to run the loop till balance becomes 0 or 200
    guess = int(input("Guess heads by entering 1 or tails by entering 2 for this coin flip."))
    balance = Guess_Check(guess,balance)

Sample output: (Not complete output as it was long)

Congrats you guessed right, You won $9.
Avalilable balanace is : 206
Congrats!! You reached your Target $200.
Ad
source: stackoverflow.com
Ad