Python练习题答案: 比萨付款【难度:1级】--景越Python编程实例训练营,1000道上机题等你来挑战

比萨付款【难度:1级】:

答案1:

def michael_pays(cost):
    return round(cost if cost < 5 else max(cost*2/3, cost-10), 2)

答案2:

def michael_pays(c):
    return round(c if c < 5 else c * 2 / 3 if c <= 30 else c - 10, 2)

答案3:

def michael_pays(c):
    return round(c if c < 5 else (c*(2/3) if c <=30 else c-10),2)

答案4:

def michael_pays(c):
    return round(max(c if c < 5 else c * 2/3, c - 10), 2)

答案5:

def michael_pays(costs):
    return round(costs - (costs >= 5)*min(costs/3., 10), 2)

答案6:

def michael_pays(costs):
    return round(costs, 2) if costs < 5 else round(max(costs-10, costs/3*2), 2)

答案7:

def michael_pays(costs):
    kate = round((costs/3), 2)
    if costs < 5:
        return round(costs, 2)
    return round(costs - kate, 2) if kate <= 10 else round(costs - 10, 2)

答案8:

def michael_pays(costs):
    micheal=0
    if costs<5:
        micheal = costs
    else:
        if ((1/3)*costs)>10:
             micheal = costs-10
        else:
             micheal =  costs-((1/3)*costs)
    
    try:
        if isinstance(micheal,float):
             micheal = float("{0:.2f}".format(micheal))
    except:
        micheal = micheal
        
    return micheal​

答案9:

def michael_pays(costs):
    if costs < 5:
        michael = costs
    else:
        kate = min(costs / 3, 10)
        michael = costs - kate
    return round(michael, 2)

答案10:

def michael_pays(c):
    if c < 5.:
        return round(c, 2)
    elif c < 30:
        return round(c - (c * 1/3), 2)
    return round(c - 10, 2)

答案11:

michael_pays = lambda a: round(a if a < 5 else a-10 if a > 30 else a*(2/3) , 2)



Python基础训练营景越Python基础训练营QQ群

在这里插入图片描述
欢迎各位同学加群讨论,一起学习,共同成长!

原文链接:加载失败,请重新获取