繁体   English   中英

ATM程序无法在Python中正确循环

[英]ATM program not looping properly in Python

我正在制作一个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()

为了修改值SavingChecking你的functions ,你必须声明它们global函数里面,不使用global是不是最优的,因为据我所知,但似乎是你手头的任务。 通过使用while Truebreak可以简化while循环。 一路上,我发现了几个小东西,我感动了,我也考虑改变SavingSavings ,因为这是你正在接受作为响应的关键词,让你的东西更容易。

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()

暂无
暂无

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

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