繁体   English   中英

如何实现一个while循环来继续取最新的值?

[英]How to implement a while loop to continue taking the latest value?

我创建了下面的银行项目,其中 while 循环始终将总金额设为 100,并在用户 select 为“是”时继续循环。 但是,我希望 while 循环获取最新的总金额 190,这是存款后累积的金额,以便继续从中取款。 但是我无法在我的代码中实现它。 我怎样才能在下面的代码中实现它?

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如下:

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

你的总金额是多少:500

您想提取多少金额? 1个

您当前的余额是 499

你想存多少钱? 500

您的新余额是 999

你想玩“y”还是“n”?

您想提取多少金额? 1个

您当前的余额是 998

你想存多少钱? 502

你的新余额是 1500

我清理了一下东西。 关键部分是您的account_balance应该最初设置在您的游戏循环之外,并在调用您的函数时增加。 目前,您正在保留多个版本的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
①如果本文未解决您的问题,请点击查看与本文相关的问题
②如果本文未解决您的问题,请向程序员专用AI小助手提问
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM