首页 > 解决方案 > 嵌套在 elif 中的 elif 语法无效

问题描述

我目前正在为我的 CSE 课程做一个项目,我们的目的是向用户询问他们的汽车租赁信息,然后打印出他们的余额和其他信息。每个用户都有一个预算 (B)、每日 (D) 或每周 (W) 代码,它们都有不同的公式来计算帐户的最终余额。我尝试对每个代码使用 if 和 elif 语句,但是在将 if 语句嵌套在 elif 语句中时,我不断收到无效的语法错误。我该如何解决此错误以继续从事该项目?谢谢!

answer = input("Would you like to continue? (Y/N)? ")

while answer == 'Y' or answer == 'y': #user inputs information if they reply yes to the prompt
     customer_code = input("Please enter Customer code (B/D/W): ")
     days_rented = int(input("Please enter the number of days rented: "))
     initial_miles = int(input("Please enter the original mileage of the rental: "))
     final_miles = int(input("Please enter the new mileage of the rental: "))

     mileage = final_miles - initial_miles #calculating other information
     weeks = (days_rented / 7)
     average_miles_d = mileage / days_rented
     average_miles_y = mileage / weeks

     if customer_code == 'B': #budget customer
         balance = ((40 * days_rented) + (.25 * mileage))

     elif customer_code == 'D': #daily customer
         if average_miles_d < 100:
             balance = ((60 * days_rented)    
         elif average_miles_d >= 100:
             balance = ((60 * days_rented) + (.25 * (average_miles - 100)))

     elif customer_code == 'W': #weekly customer
         if average_miles_w < 900:
             balance = (190 * weeks)    
         elif average_miles_w > 900 and average_miles_w < 1500:
             balance = (100 * weeks)    
         else:
             balance = ((200 * weeks) + (.25 * (average_miles_w - 1500))

标签: pythonpython-3.xif-statement

解决方案


推荐阅读