我正在制作一个ATM类型的程序,在该程序中,我必须询问用户是否要存入,提取或检查其储蓄或支票帐户中的余额,但前提是输入的密码是1234。已被指示使用全局变量并将储蓄和支票初始化为0。我的所有功能均正常运行,但是当程序再次循环时,即使用户刚刚将钱存入任一帐户,储蓄和支票余额仍为0。 。我不确定我的代码的哪一部分搞砸了,但是任何帮助将不胜感激。
#Create global variables
Checking = 0
Saving = 0
def main():
pin_number = input("Please enter your pin number ")
stop = False
while not is_authorized(pin_number) and stop!= True:
if pin_number == "0":
stop = True
if pin_number == "1234":
stop = False
if stop != True:
while True:
choice = display_menu()
if choice == 1:
deposit()
elif choice == 2:
withdraw()
elif choice == 3:
check_balance()
def is_authorized (pin_number):
if pin_number == "1234":
return True
else:
return False
def display_menu():
print('1) Deposit')
print('2) Withdraw')
print('3) Check amount')
choice = int(input("Please enter the number of your choice: "))
return choice
def deposit():
depositing=str(input("Would you like to deposit into your savings
or checking? "))
if depositing == "savings":
depo = float(input("How much would you like to deposit? "))
new_savings = Saving + depo
print ("Your new balance is" +str (new_savings))
elif depositing == "checkings":
depo = input(int("How much would you like to deposit? "))
new_checking = Checking + depo
print ("Your new balance is" +str (new_checking))
def withdraw():
print ("Your savings account balance is " +str (Saving))
print ("Your checkings account balance is " +str (Checking))
withdrawing=str(input("Would you like to withdraw from your checking or savings? "))
if withdrawing == "checking":
withdraw = int(input("How much would you like to withdraw? "))
checking2= Checking - withdraw
print ("Your new balance is" +str (checking2))
elif withdrawing == "savings":
withdraw = int(input("How much would you like to withdraw? "))
savings2= Saving - withdraw
print ("Your new balance is" +str (savings2))
def check_balance():
checkbalance= input("Would you like to check your savings or checking? ")
if checkbalance == "savings":
print (Saving)
elif checkbalance == "checking":
print ("Your balance is $" +str (Checking))
main()
答案 0 :(得分:0)
要在Saving
中修改值Checking
和functions
,必须在函数内声明global
,而不是使用global
据我所知,这不是最佳选择,但似乎这是您的任务。通过使用while True
和break
,可以简化while循环。在发现一些小问题的过程中,我还考虑将Saving
更改为Savings
,因为这是您接受作为响应的关键字,使事情变得更容易。
Checking = 0
Saving = 0
def main():
while True:
pin_number = input("Please enter your pin number ")
while pin_number not in ('0','1234'):
pin_number = input("Please enter your pin number ")
if pin_number == "0":
break
elif pin_number == "1234":
choice = display_menu()
if choice == 1:
deposit()
elif choice == 2:
withdraw()
elif choice == 3:
check_balance()
def is_authorized (pin_number):
if pin_number == "1234":
return True
else:
return False
def display_menu():
print('1) Deposit')
print('2) Withdraw')
print('3) Check amount')
choice = int(input("Please enter the number of your choice: "))
return choice
def deposit():
global Checking, Saving
depositing=str(input("Would you like to deposit into your savings or checking? "))
if depositing == "savings":
depo = float(input("How much would you like to deposit? "))
Saving += depo # use += here
print ("Your new balance is " +str (Saving))
elif depositing == "checking": # changed to checking
depo = int(input("How much would you like to deposit? "))
Checking += depo
print ("Your new balance is " +str (Checking))
def withdraw():
global Checking, Saving
print ("Your savings account balance is " +str (Saving))
print ("Your checkings account balance is " +str (Checking))
withdrawing=str(input("Would you like to withdraw from your checking or savings? "))
if withdrawing == "checking":
withdraw = int(input("How much would you like to withdraw? "))
Checking -= withdraw
print ("Your new balance is " +str (Checking))
elif withdrawing == "savings":
withdraw = int(input("How much would you like to withdraw? "))
Saving -= withdraw
print ("Your new balance is " +str (Saving))
def check_balance():
global Checking, Saving
checkbalance= input("Would you like to check your savings or checking? ")
if checkbalance == "savings":
print (Saving)
elif checkbalance == "checking":
print ("Your balance is $" +str (Checking))
main()