python - 如何检查单元检查中的错误?
问题描述
23.11 LAB:收费计算
收费公路根据一天中的时间和周末收取不同的费用。编写一个函数 calc_toll(),它具有三个参数:当前时间(int)、时间是否是早上(布尔值)以及当天是否是周末(布尔值)。该函数根据下表返回正确的通行费(浮动)。
Weekday Tolls
Before 7:00 am ($1.15)
7:00 am to 9:59 am ($2.95)
10:00 am to 2:59 pm ($1.90)
3:00 pm to 7:59 pm ($3.95)
Starting 8:00 pm ($1.40)
Weekend Tolls
Before 7:00 am ($1.05)
7:00 am to 7:59 pm ($2.15)
Starting 8:00 pm ($1.10)
例如:下面的函数调用,带有给定的参数,将返回以下通行费:
calc_toll(8, True, False) returns 2.95
calc_toll(1, False, False) returns 1.90
calc_toll(3, False, True) returns 2.15
calc_toll(5, True, True) returns 1.05
我的代码是
def call_toll(a,b,c):
#if weekend
if(c):
#moring
if(b):
if(a<7):
#if time is before 7
return 1.05
#if time is after 7
else:
return 2.15
else:
#if time is after 8 not in morning
if(a>=8):
return 1.10
#if time is before 8 not in morning
else:
return 2.15
else:
#if time in morning
if(b):
if(a<7):
#if time is before 7
return 1.15
#if time is in between
elif(a>7 and a<10):
return 2.95
else:
return 1.90
else:
#not in morning
if(a>=8):
return 1.40
#if time is in between 3 to 8
elif(a>=3 and a<8):
return 3.95
else:
return 1.90
print(call_toll(8,True,False))
print(call_toll(1,False,False))
print(call_toll(3,False,True))
print(call_toll(5,True,True))
有一个单元测试我没有通过
解决方案
推荐阅读
- javascript - 如何在角度 6 中显示多个行值的总数以分隔表格的列?
- reactjs - React 没有将状态传递给 mapStateToProps
- c# - 循环后是否需要调用`yield break`?
- r - 如何避免用户定义函数的命名空间歧义
- tensorflow - 如何在 TPU 上实现布尔屏蔽
- python - 我现在应该怎么做才能解决这个问题,因为它显示 x 未定义?
- android - 如何从 webview 中的存储访问 IndexedDB?
- python - Python Tkinter“exportselection”在 Windows 机器上不起作用
- python - 获取列表中最小项的索引
- php - 将包含空格的字符串作为响应从 php 发送到 Android