首页 > 解决方案 > 如何检查单元检查中的错误?

问题描述

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))

有一个单元测试我没有通过

标签: python

解决方案


推荐阅读