I have created the below bank project where the while loop is always taking the total amount as 100 and continue the loop as long as user select 'yes'. However I would like the while loop to take the latest total amount 190 which is the amount accumulated after deposit to continue withdraw money from. However I am unable to implement that in my code. How can I be able to implement that in the below code?
def withdrew(t, w):
if t>w:
balance=int(t-w)
return balance
else:
return ("Not enough money to wthdrew.")
def deposit(d):
current_balance=withdrew(t, w)
new_balance = current_balance d
return new_balance
t=int(input("What is your total amount: "))
is_game_on= True
while is_game_on:
w=int(input("How much amount you would like to withdrew? "))
withdraw_money=withdrew(t, w)
print(f"Your current balance is {withdraw_money}")
d = int(input("How much you would like to deposit? "))
deposit_money=deposit(d)
print(f"Your new balance is {deposit_money}")
user=input("Would you like to play-'y'or 'n'? ").lower()
if user=='y':
is_game_on=True
else:
is_game_on=False
Output as below:
What is your total amount: 100
How much amount you would like to withdrew? 10
Your current balance is 90
How much you would like to deposit? 100
Your new balance is 190
Would you like to play-'y'or 'n'? y
How much amount you would like to withdrew? 10
Your current balance is 90
CodePudding user response:
I cleaned things up a bit. The critical part is that your account_balance
should be set initially outside of your game loop and incremented as a result of calling your functions. At the moment you were kind of keeping several versions of your account_balanace
.
def withdraw(t, w):
if t < w:
raise ValueError("You cannot withdraw more money than is in your account.")
return t-w
def deposit(t, d):
return t d
account_balance = int(input("What is your total amount: "))
while True:
withdraw_amount = int(input("How much amount you would like to withdraw? "))
account_balance = withdraw(account_balance, withdraw_amount)
print(f"Your current balance is {account_balance}")
deposit_amount = int(input("How much you would like to deposit? "))
account_balance = deposit(account_balance, deposit_amount)
print(f"Your new balance is {account_balance}")
user=input("Would you like to play-'y'or 'n'? ").lower()
if user != 'y':
break
CodePudding user response:
def withdrew(bank, w):
if bank>w:
bank=int(bank-w)
return bank
else:
print("Not enough money to wthdrew.")
return bank
def deposit(bank, d):
return bank d
t=int(input("What is your total amount: "))
bank = t
is_game_on= True
while is_game_on:
w=int(input("How much amount you would like to withdrew? "))
bank=withdrew(bank, w)
print(f"Your current balance is {bank}")
d = int(input("How much you would like to deposit? "))
bank=deposit(bank, d)
print(f"Your new balance is {bank}")
user=input("Would you like to play-'y'or 'n'? ").lower()
if user=='y':
is_game_on=True
else:
is_game_on=False
What is your total amount: 500
How much amount you would like to withdrew? 1
Your current balance is 499
How much you would like to deposit? 500
Your new balance is 999
Would you like to play-'y'or 'n'? y
How much amount you would like to withdrew? 1
Your current balance is 998
How much you would like to deposit? 502
Your new balance is 1500