Python 程序问题
因此,我创建了一个程序,要求用户从他们的支票账户中提取他们想要的金额。如果执行该程序,则支票应减去提取的金额。但是当我使用global时,即使在这个过程之后,它也不会改变变量的值。我已经建立了所有变量的值 我的代码在这里:Python 程序问题,python,variables,error-handling,procedure,Python,Variables,Error Handling,Procedure,因此,我创建了一个程序,要求用户从他们的支票账户中提取他们想要的金额。如果执行该程序,则支票应减去提取的金额。但是当我使用global时,即使在这个过程之后,它也不会改变变量的值。我已经建立了所有变量的值 我的代码在这里: checking = 10000 savings = 10000 user_ammount_w = 0 user_currency_w = "" def withdraw_saving (amount, country): global checking if
checking = 10000
savings = 10000
user_ammount_w = 0
user_currency_w = ""
def withdraw_saving (amount, country):
global checking
if country == "HKD":
if checking >= amount:
checking = checking - amount
print("The amount of money left in your checking is", checking)
else:
print("Your request of", "$"+ str(amount), country, "is greater than the amount in your
checking account this withdraw will not work")
user_choice = input("Welcome to the ATM. Type 1 for withdrawing")
if user_choice == "1":
user_currency_w= input("Which currency would you like to withdraw from. For testing purposes
its only HKD")
user_amount_w= int(input("How much money do you want to withdraw"))
withdraw_saving (user_ammount_w, user_currency_w)
当您再次递归调用函数时,当您传入全局设置为
0
的user\u amount\u w
时,您都将要减去的金额传递为0。相反,我怀疑您想要传入user\u amount\w
,这是用于捕获用户输入的变量名
checking = 10000
savings = 10000
user_ammount_w = 0 #<----this is the value you passing to your function
user_currency_w = ""
def withdraw_saving (amount, country):
global checking
if country == "HKD":
if checking >= amount:
checking = checking - amount
print("The amount of money left in your checking is", checking)
else:
print("Your request of", "$"+ str(amount), country, "is greater than the amount in your checking account this withdraw will not work")
user_choice = input("Welcome to the ATM. Type 1 for withdrawing")
if user_choice == "1":
user_currency_w= input("Which currency would you like to withdraw from. For testing purposes its only HKD")
user_amount_w= int(input("How much money do you want to withdraw")) #<--you never pass this value.
withdraw_saving (user_ammount_w, user_currency_w)
withdraw_saving(20, 'HKD')
检查=10000
节省=10000
用户\u amount\u w=0#=金额:
支票=支票-金额
打印(“支票中的剩余金额为”,支票)
其他:
打印(“您对“,“$”+str(金额),国家/地区的请求”大于您的支票账户中本次提款将不起作用的金额”)
用户选择=输入(“欢迎使用自动取款机。输入1用于取款”)
如果用户_选项==“1”:
user_currency_w=input(“您希望从哪种货币中提取。出于测试目的,其仅为港币”)
user\u amount\u w=int(输入(“您想提取多少钱”)#查看user\u amount\u w
vs.user\u amount\u w
。