我想创建一个名为bank的函数。我输入了称为money的信息,询问您要从第二个变量“ storage”中取出多少钱。存储= 10000,如果存储> 0:减去存储-钱。 如果存储<= 0: 打印(“我们没有钱”),然后退出该功能
def bank():
money = int(input("How much money you want? "))
storage = 10000
if storage > 0:
storage = storage - money
print(storage)
bank()
if storage <= 0:
print("we dont have money")
quit()
bank()
问题是,如果金钱== 1000,那么存储== 9000,但是如果第二次金钱== 2000,则存储应该显示7000,但显示8000
答案 0 :(得分:1)
每次您递归调用bank()
时,它都会将存储重置为10000。因此,您需要在功能之外进行初始化。
所以
def bank(storage):
money = int(input("How much money you want? "))
if storage > 0:
storage = storage - money
print(storage)
bank(storage)
if storage <= 0:
print("we dont have money")
quit()
storage = 10000
bank(storage)