即使输出中没有任何错误,我的 print 语句也不会运行,并且很难跟踪错误



blockquote

Age = int(input("Enter your age"))
Day = input("WD or WE")
Upgrade = input("Do you want to upgrade?")
Month = input("Enter the month you're going in")
#Age_Group
if Age > 12:
Age_Group = "Adult"
else:
Age_Group = "Child"
#Month
if Month == "November":
if Age_Group == "Adult" and Day == "WD":
Price = 46
elif Age_Group == "Adult" and Day == "WE":
Price = 55
elif Age_Group == "Child" and Day == "WD":
Price = 34
elif Age_Group == "Child" and Day == "WE":
Price = 42
else:
if Age_Group == "Adult" and Day == "WD":
Price = 69
elif Age_Group == "Adult" and Day == "WE":
Price = 79
elif Age_Group == "Child" and Day == "WD":
Price = 53
elif Age_Group == "Child" and Day == "WE":
Price = 59
if Upgrade == "Yes":
if Age_Group == "Adult" and Day == "WD":
FinalPrice = Price + 46
print(f"You have to pay {FinalPrice}")
elif Age_Group == "Adult" and Day == "WE":
FinalPrice = Price +  55
print(f"You have to pay {FinalPrice}")

elif Age_Group == "Child" and Day == "WD":
FinalPrice = Price + 34
print(f"You have to pay {FinalPrice}")

elif Age_Group == "Child" and Day == "WE":
FinalPrice = Price + 42
print(f"You have to pay {FinalPrice}")
else:
if Age_Group == "Adult" and Day == "WD":
FinalPrice = Price 
print(f"You have to pay {FinalPrice}")
elif Age_Group == "Adult" and Day == "WE":
FinalPrice = Price 
print(f"You have to pay {FinalPrice}")
elif Age_Group == "Child" and Day == "WD":
FinalPrice = Price 
print(f"You have to pay {FinalPrice}")
elif Age_Group == "Child" and Day == "WE":
FinalPrice = Price 
print(f"You have to pay {FinalPrice}")

这是我第一次在堆栈溢出上发帖,所以如果我做得对,我不知道如何正确格式化帖子或ir,所以请耐心等待;15〃"WE"是";和十一月,因此print语句不会运行,代码也不会输出任何内容,但我一生都找不到错误的来源,输出中缺少错误也于事无补。这是一条不会运行的线路

elif Age_Group==";"成年人";并且Day=";WE":价格=55

条件'elif Age_Group=="成年人";并且Day=";WE":Price=55’属于"if Upgrade="的范围;是的":"。这就是为什么你的elif条件没有被调用。您希望将您的elif保持在"if Age_Group=="的范围内;"成年人";并且Day=";WD":'其刚好低于"if Upgrade==";是的":"。以下将起作用:

if Upgrade == "Yes":
if Age_Group == "Adult" and Day == "WD":
FinalPrice = Price + 46
print(f"You have to pay {FinalPrice}")
elif Age_Group == "Adult" and Day == "WE":
FinalPrice = Price + 55
print(f"You have to pay {FinalPrice}")
elif Age_Group == "Child" and Day == "WD":
FinalPrice = Price + 34
print(f"You have to pay {FinalPrice}")
elif Age_Group == "Child" and Day == "WE":
FinalPrice = Price + 42
print(f"You have to pay {FinalPrice}")
else:
if Age_Group == "Adult" and Day == "WD":
FinalPrice = Price
print(f"You have to pay {FinalPrice}")
elif Age_Group == "Adult" and Day == "WE":
FinalPrice = Price
print(f"You have to pay {FinalPrice}")
elif Age_Group == "Child" and Day == "WD":
FinalPrice = Price
print(f"You have to pay {FinalPrice}")
elif Age_Group == "Child" and Day == "WE":
FinalPrice = Price
print(f"You have to pay {FinalPrice}")

问题是您的缩进不正确,这很难看出,因为代码的格式(和缩进(不规则:

if Upgrade == "Yes":
if Age_Group == "Adult" and Day == "WD":
FinalPrice = Price + 46
print(f"You have to pay {FinalPrice}")
elif Age_Group == "Adult" and Day == "WE":
FinalPrice = Price +  55
print(f"You have to pay {FinalPrice}")

在这里,如果我们正确地缩进代码(到4个空格(,我们就会得到

if Upgrade == "Yes":
if Age_Group == "Adult" and Day == "WD":
FinalPrice = Price + 46
print(f"You have to pay {FinalPrice}")
elif Age_Group == "Adult" and Day == "WE":
FinalPrice = Price +  55
print(f"You have to pay {FinalPrice}")

也就是说,如果"Upgrade"是"0";是";,则它将检查年龄组是否为"0";"成年人";而这一天是";WD";。既然事实并非如此(这一天是"WE"(,什么都不会发生。

由于下面的elif子句和最后的else子句都是非意图的,所以它们被附加到if Upgrade子句中,因此被跳过。您应该正确缩进内部if,并重新缩进所有elif以匹配:

if Upgrade == "Yes":
if Age_Group == "Adult" and Day == "WD":
FinalPrice = Price + 46
print(f"You have to pay {FinalPrice}")
elif Age_Group == "Adult" and Day == "WE":
FinalPrice = Price +  55
print(f"You have to pay {FinalPrice}")

elif Age_Group == "Child" and Day == "WD":
FinalPrice = Price + 34
print(f"You have to pay {FinalPrice}")

elif Age_Group == "Child" and Day == "WE":
FinalPrice = Price + 42
print(f"You have to pay {FinalPrice}")
else:
...

核心架构也使这类错误更有可能发生:整个过程完全是基于年龄、DoW和月份的数据驱动,还有一个额外的升级步骤。因此,通过将价格确定定义为一个表(或哈希图(,例如,整个过程将大大简化

from enum import Enum
Age = int(input("Enter your age"))
Day_ = input("WD or WE")
Upgrade = input("Do you want to upgrade?")
Month = input("Enter the month you're going in")
class AgeGroup(Enum):
ADULT = 1
CHILD = 2
class Day(Enum):
WE = 1
WD = 2
TABLE = {
"November": {
(AgeGroup.ADULT, Day.WD): 56,
(AgeGroup.ADULT, Day.WE): 55,
(AgeGroup.CHILD, Day.WD): 34,
(AgeGroup.CHILD, Day.WE): 42,
},
None: { # defaults
(AgeGroup.ADULT, Day.WD): 69,
(AgeGroup.ADULT, Day.WE): 79,
(AgeGroup.CHILD, Day.WD): 53,
(AgeGroup.CHILD, Day.WE): 59,
},
}
UPGRADES = {
(AgeGroup.ADULT, Day.WD): 46,
(AgeGroup.ADULT, Day.WE): 55,
(AgeGroup.CHILD, Day.WD): 34,
(AgeGroup.CHILD, Day.WE): 42,
}
age_group = AgeGroup.ADULT if Age > 12 else AgeGroup.CHILD
day = Day[Day_]
prices = TABLE.get(Month) or TABLE[None]
price = prices[age_group, day]
if Upgrade == 'Yes':
price += UPGRADES[age_group, day]
print(f"You have to pay {price}")

拥有数据驱动的结构使更容易:

  • 捕获错误(hashmaps会在不正确的密钥访问时引发异常,而限制条件会使它们更难出错并无意中跳过代码(
  • 检查所有条件是否都有大小写并且值是否正确
  • 最终从代码中嵌入的数据迁移到外部纯数据文件中的数据,这将使监督和更新更容易

最新更新