【问题标题】:Python ATM Else and Elif ErrorsPython ATM Else 和 Elif 错误
【发布时间】:2014-06-30 05:21:27
【问题描述】:

我最近在 Python 3.3.3 中遇到了 Else 和 Elif 语句的一些问题。

这是我的代码:

# ATM was programmed by Jamie Mathieson

# introduction

sleep (1)

print ("-----------------------------------------------")
print ("\n                  ATM                ")
print ("\n-----------------------------------------------")

sleep (3)

print ("\nWelcome to ATM. ATM is a mathematical system that handles data.")

sleep (5)

print ("\n Your ATM card has is being inserted. Please wait...")

sleep (3)

print ("Your ATM card has been inserted.")

sleep (5)

print ("Type 'options' to view available commands.")

# variables

balance = print("Balance  £", money)

money = 200

options = ("Options: 1) Withdraw <amount> 2) Deposit <amount> 3) Balance 4) Exit")

# statements

option=int(input("Please enter an option: "))

if Option==1:
    print("Balance  £", money)

if Option==2:
    print("Balance  £", money)
    Withdraw=float(input("Please enter the amount of money you would like to withdraw: £ "))
    if Withdraw>0:
        newbalance=(money-Withdraw)
        print("New Balance: £",remainingbalance)
        elif: Withdraw>money
            print("No Balance Remaining")
        else:
            print("Withdraw canceled.")

if Option==3:
    print("Balance  £", money)
    Deposit=float(input("Please enter the amount of money you would like to deposit: £ "))
    if Deposit>0:
        newbalance=(money+Deposit)
            print("New Balance: £",newbalance)
    else:
            print("Deposit canceled.")

if Option==4:
    print("ATM is ejecting your card. Please wait...")
    sleep(5)
    exit()

我得到的错误是“无效的语法”,它突出显示了 Else 和 Elif 语句。我做错了什么?

【问题讨论】:

    标签: python if-statement


    【解决方案1】:

    您必须将 : 放在最后,并更正标识。

    if Option==2:
        print("Balance  £", money)
        Withdraw=float(input("Please enter the amount of money you would like to withdraw: £ "))
        if Withdraw>0:
           newbalance=(money-Withdraw)
           print("New Balance: £",remainingbalance)
        elif Withdraw>money:
            print("No Balance Remaining")
        else:
            print("Withdraw canceled.")
    

    【讨论】:

    • 但是,出于某种原因,elif 似乎仍然是一个错误。尝试将脚本作为 .py 文件运行!
    • 哦,我明白了!虽然, sleep() 没有定义。我将如何导入它?
    • 它在time中定义,所以你可以做from time import sleep;但把它拿出来,你的用户会更快乐。让代码运行得更慢很烦人。
    【解决方案2】:

    代码有几个问题。正如@Daniel 指出的那样,您的缩进必须更正。此外,elif 块的条件放在冒号之后。

    除此之外,您将用户的响应分配给名为option 的变量,然后在Option 上写入条件。这是两个不同的东西。

    最后balance = print("Balance £", money) 会抛出一个错误。看起来您正在尝试将balance 定义为一个函数,该函数将打印“Balance £”,后跟余额。如果是这样,你可以这样做:

    balance = lambda x: print("Balance  £{}".format(x))
    

    编辑:要回答您的问题:sleep,请使用

    from time import sleep
    

    【讨论】:

    • 而且,我将如何做到这一点,以便用户选择一个选项后,可以提示他们选择另一个选项,或者打印一条消息说“感谢您使用 ATM。您的卡现在正在被弹出。请稍候..." - 让脚本继续运行的东西。
    • 为此,您需要将代码放入循环中。我建议使用while(True) 构造以及在用户输入导致退出的选项时执行的break 语句。
    • 能否请您提交一个示例到 pastebin 链接?我只了解循环的基础知识。
    • 我无法在我的办公室使用 pastebin,但这应该可以作为我所说的一个例子:pythonfiddle.com/sample-while-loop
    【解决方案3】:
    while True:
    
           # Reading id from user
           id = int(input("\nEnter account pin: "))
     
           # Loop till id is valid
           while id < 1000 or id > 9999:
               id = int(input("\nInvalid Id.. Re-enter: "))
     
           # Iterating over account session
           while True:
     
               # Printing menu
               print("\n1 - View Balance \t 2 - Withdraw \t 3 - Deposit \t 4 - Exit ")
     
               # Reading selection
               selection = int(input("\nEnter your selection: "))
     
               # Getting account object
               for acc in accounts:
                   # Comparing account id
                   if acc.getId() == id:
                       accountObj = acc
                       break
     
               # View Balance
               if selection == 1:
                   # Printing balance
                   print(accountObj.getBalance())
     
               # Withdraw
               elif selection == 2:
                   # Reading amount
                   amt = float(input("\nEnter amount to withdraw: "))
                   ver_withdraw = input("Is this the correct amount, Yes or No ? " + str(amt) + " ")
     
                   if ver_withdraw == "Yes":
                       print("Verify withdraw")
                   else:
                       break
     
                   if amt < accountObj.getBalance():
                      # Calling withdraw method
                      accountObj.withdraw(amt)
                      # Printing updated balance
                      print("\nUpdated Balance: " + str(accountObj.getBalance()) + " n")
                   else:
                        print("\nYou're balance is less than withdrawl amount: " + str(accountObj.getBalance()) + " n")
                        print("\nPlease make a deposit.");
     
               # Deposit
               elif selection == 3:
                   # Reading amount
                   amt = float(input("\nEnter amount to deposit: "))
                   ver_deposit = input("Is this the correct amount, Yes, or No ? " + str(amt) + " ")
     
                   if ver_deposit == "Yes":
                      # Calling deposit method
                      accountObj.deposit(amt);
                      # Printing updated balance
                      print("\nUpdated Balance: " + str(accountObj.getBalance()) + " n")
                   else:
                       break
     
               elif selection == 4:
                   print("nTransaction is now complete.")
                   print("Transaction number: ", random.randint(10000, 1000000))
                   print("Current Interest Rate: ", accountObj.annualInterestRate)
                   print("Monthly Interest Rate: ", accountObj.annualInterestRate / 12)
                   print("Thanks for choosing us as your bank")
                   exit()
     
               # Any other choice
               else:
                   print("That's an invalid choice.")
     
    # Main function
    main()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 2017-07-03
      • 2012-09-01
      • 2016-07-11
      相关资源
      最近更新 更多