[英]How to implement a while loop to continue taking the latest value?
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'.我创建了下面的银行项目,其中 while 循环始终将总金额设为 100,并在用户 select 为“是”时继续循环。 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.
但是,我希望 while 循环获取最新的总金额 190,这是存款后累积的金额,以便继续从中取款。 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: Output如下:
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
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你的总金额是多少:500
How much amount you would like to withdrew?您想提取多少金额? 1
1个
Your current balance is 499您当前的余额是 499
How much you would like to deposit?你想存多少钱? 500
500
Your new balance is 999您的新余额是 999
Would you like to play-'y'or 'n'?你想玩“y”还是“n”? y
是
How much amount you would like to withdrew?您想提取多少金额? 1
1个
Your current balance is 998您当前的余额是 998
How much you would like to deposit?你想存多少钱? 502
502
Your new balance is 1500你的新余额是 1500
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.关键部分是您的
account_balance
应该最初设置在您的游戏循环之外,并在调用您的函数时增加。 At the moment you were kind of keeping several versions of your account_balanace
.目前,您正在保留多个版本的
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.