修正以下python代码:class Ticket: def __init__(self, day, discount=False): self.day = day self.discount = discount def get_ticket_price(self, num): if self.day == "周一": print("博物馆周一闭馆,不提供售票服务") elif self.day == "周二" or "周三" or "周四" or "周五": if self.discount: price = 80 else: price = 100 print("总共需支付票价{}".format(num * price)) elif self.day == "周六" or "周日": if self.discount: price = 88 else: price = 110 print("总共需支付票价{}".format(num * price))
时间: 2024-02-14 10:03:22 浏览: 66
修正后的代码如下:
```
class Ticket:
def __init__(self, day, discount=False):
self.day = day
self.discount = discount
def get_ticket_price(self, num):
if self.day == "周一":
print("博物馆周一闭馆,不提供售票服务")
elif self.day in ["周二", "周三", "周四", "周五"]:
if self.discount:
price = 80
else:
price = 100
print("总共需支付票价{}".format(num * price))
elif self.day in ["周六", "周日"]:
if self.discount:
price = 88
else:
price = 110
print("总共需支付票价{}".format(num * price))
```
修改的内容如下:
- 在第8行,将 `elif self.day == "周二" or "周三" or "周四" or "周五"` 改为 `elif self.day in ["周二", "周三", "周四", "周五"]`,因为原来的表达式会被解释为 `elif (self.day == "周二") or ("周三") or ("周四") or ("周五")`,这样的结果是始终为 `True`,因为非空字符串被视为 `True`。
- 在第13行,将 `elif self.day == "周六" or "周日"` 改为 `elif self.day in ["周六", "周日"]`,原因同上。
阅读全文