第29006题 单选题
下列哪个代码片段可以正确实现简易公交刷卡扣费的模拟功能?

已知成人公交单次票价为2元,学生卡享受5折优惠,老年卡免费。程序需要接收两个输入参数:第一个参数为乘客类型(1代表成人,2代表学生,3代表老年),第二个参数为本次刷卡的总次数。以下四个Python代码片段中,哪一个可以正确计算并返回本次刷卡的总扣费金额?

A
def calculate_fee(type, times):
    price = 2
    if type == 1:
        return price * times
    elif type == 2:
        return price * 0.8 * times
    elif type ==3:
        return 0
B
def calculate_fee(type, times):
    price = 2
    if type == 1:
        return price * times
    elif type == 2:
        return price * 0.5 * times
    elif type ==3:
        return 1 * times
C
def calculate_fee(type, times):
    price = 2
    if type == 1:
        return price * times
    elif type == 2:
        return price * 0.5 * times
    elif type ==3:
        return 0
D
def calculate_fee(type, times):
    price = 2
    if type == 1:
        return price * times * 0.5
    elif type == 2:
        return price * times
    elif type ==3:
        return 0
程序运行统计
暂无判题统计
提交0次 正确率0.00%
答案解析