程序问题
Procedure issues
所以我创建了一个程序,询问用户他们想从他们的支票账户中提取多少钱。如果执行该程序,检查应减去提取的金额。但是当我使用 global 时,即使在 procedure.I 已经建立了所有变量
之后,它也没有改变变量的值
我的代码在这里:
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_ammount_w
一样,您将其全局设置为 0
。相反,我怀疑你想传入 user_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')
然而,使用全局变量并不是一个好主意,可能有更好的设计方法。
所以我创建了一个程序,询问用户他们想从他们的支票账户中提取多少钱。如果执行该程序,检查应减去提取的金额。但是当我使用 global 时,即使在 procedure.I 已经建立了所有变量
之后,它也没有改变变量的值我的代码在这里:
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_ammount_w
一样,您将其全局设置为 0
。相反,我怀疑你想传入 user_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')
然而,使用全局变量并不是一个好主意,可能有更好的设计方法。