python - 嵌套在 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))
解决方案
推荐阅读
- r - R SUR 回归
- date - 在 DATE 列中用 LIKE 续集
- python - 如何同步两个步进电机.. 使用 DRV8825
- datetime - BigQuery:如何从现有的日期时间列中提取年份并作为新的特定年份列插入表中?
- java - 即使设置了 NamespaceContext 也无法获取标签的文本
- python - mongoengine 取消引用嵌套文档
- wordpress - 从 Wordpress oembed json 链接中删除 author 和 author_url
- jquery - 如何使全日历中的事件以 12 小时而不是 24 小时格式显示
- pyspark - Spark Sql 从 Hive orc 分区表中读取,给出数组越界异常
- git - 计算自 fork 以来 Git 上的提交次数