Python練習題答案: 燃料使用情況報告【難度:2級】--景越Python編程實例訓練營,1000道上機題等你來挑戰

燃料使用情況報告【難度:2級】:

答案1:

def total_kilometers(cons, petrol):
    return round(100*petrol/cons, 2)

def check_distance(dist, cons, petrol):
    return ("You will need to refuel" if dist > total_kilometers(cons, petrol) else
            [ [n*100, dist-100*n, round(petrol-cons*n, 2)] for n in range(dist//100+1)])

答案2:

def total_kilometers(cons, petrol):
    return round(petrol * 100.0 / cons, 2)

def check_distance(distance, cons, petrol):
    if total_kilometers(cons, petrol) < distance:
        return 'You will need to refuel'
    return [[round(x, 2) for x in (i * 100, distance - i * 100, petrol - cons * i)] for i in range(int(distance / 100) + 1)]

答案3:

def total_kilometers(cons, petrol):
    return round(float(petrol) / cons * 100, 2)

def check_distance(distance, cons, petrol):
    if distance * cons > petrol * 100: return "You will need to refuel"
    return [[i, distance - i, round(petrol - i * cons / 100, 2)] for i in xrange(0, distance + 1, 100)]

答案4:

def total_kilometers(cons, petrol):
    return round(petrol * 100.0 / cons,2)

def check_distance(distance, cons, petrol):
    return [[(100 * n), distance - (100 * n), round(petrol - (cons * n),2)] for n in range(distance/100 + 1)] if total_kilometers(cons, petrol) >= distance else "You will need to refuel"

答案5:

def total_kilometers(cons, petrol):
    return round(petrol / float(cons) * 100, 2)


def check_distance(distance, cons, petrol):
    if total_kilometers(cons, petrol) < distance:
        return 'You will need to refuel'
    km_driven = 0
    result = []
    while distance >= 0:
        result.append([km_driven, distance, petrol])
        distance -= 100
        km_driven += 100
        petrol = round(petrol - cons, 2)
    return result
​

答案6:

def total_kilometers(cons, petrol):
    return round(100 * petrol / cons, 2)

def check_distance(distance, cons, petrol):
    if total_kilometers(cons, petrol) < distance: return "You will need to refuel"
    return [[i, distance-i, round(petrol-i//100*cons, 2)] for i in range(0, distance+1, 100)]

答案7:

def total_kilometers(cons, petrol):
    return round(petrol/cons*100,2)

def check_distance(distance, cons, petrol):
    if total_kilometers(cons,petrol)<distance:
        return "You will need to refuel"
    else:
        return [[path, distance-path, round(petrol-path*cons/100,2)] for path in range(0,distance+1,100)]

答案8:

def total_kilometers(cons, petrol):
    return round(petrol * 100 / cons, 2)

def check_distance(distance, cons, petrol):
    if distance > total_kilometers(cons, petrol): return 'You will need to refuel'
    
    res = []
    for i in range(0, distance + 1, 100):
        res.append([i, distance - i, petrol])
        petrol = round(petrol - cons, 2)
    return res​

答案9:

f=total_kilometers=lambda c,p:round(p/c*100,2)
check_distance=lambda d,c,p,a=100:f(c,p)<d and"You will need to refuel"or[[a*i,d-a*i,round(p-c*i,2)]for i in range(d//a+1)]

答案10:

f=total_kilometers=lambda c,p:round(p/c*100,2)

def check_distance(d,c,p,z=0):
    if f(c,p)<d:return"You will need to refuel"
    Z=[]
    while 0<=d:
        Z+=[[z,d,round(p,2)]]
        d,z,p=d-100,z+100,p-c
    return Z​




Python基礎訓練營景越Python基礎訓練營QQ羣

在這裏插入圖片描述
歡迎各位同學加羣討論,一起學習,共同成長!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章