Question 1
1. while 与 for
使用循环得到 0~10 的偶数,存储到 even_lst 列表中。
要求分别使用 while、for 实现。
2. if
You are speeding on a highway and a police officer stops you. Write a code to compute the fine that you might have to pay (call the variable fine), based on the following rules (assume that the speed is a positive integer):
(i) If your speed is 110 km/h or less, the fine is zero.
(ii) If your speed is between 111 and 130 inclusive, the fine is $150.
(iii) If your speed is above 130 km/h, the fine is $350.
(iv) If it is your birthday, your speed can be 5 km/h higher in all cases(use your actual birthday, or make one up)
For instance, if you drive 114 km/h on your birthday, your fine is zero.If you drive 118 km/h on your birthday, your fine $150. If you drive 114 km/h and it’s not your birthday, your fine is $150.
你正在高速公路上超速,一名警察拦下了你。根据以下规则编写代码来计算你可能需要支付的罚款(将该变量命名为fine
),假设速度为正整数:
(i) 如果你的速度是110 km/h或更低,罚款为零。
(ii) 如果你的速度在111到130 km/h之间(包括两者),罚款是150美元。
(iii) 如果你的速度超过130 km/h,罚款是350美元。
(iv) 如果是你的生日,你的速度在所有情况下都可以比正常高出5 km/h(使用你实际的生日,或者随便编一个)。
例如,如果你在生日当天以114 km/h的速度驾驶,你的罚款是零。如果你在生日那天以118 km/h的速度驾驶,你的罚款是150美元。如果你以114 km/h的速度驾驶且不是你的生日,你的罚款是150美元。
# 定义一个函数来计算罚款金额
def compute_fine(speed, is_birthday):
# 如果是生日,车速可以增加5km/h而不被罚款
if is_birthday:
speed -= 5
# 根据车速判断罚款金额
# 如果车速小于等于110km/h, 罚款为0
if speed <= 110:
fine = 0
# 如果车速在111km/h到130km/h之间(包括边界值),罚款为150
elif 111 <= speed <= 130:
fine = 150
# 如果车速超过130km/h, 罚款为350
else:
fine = 350
# 返回计算的罚款金额
return fine
# 示例测试
# 如果你在生日那天以114km/h的速度驾驶,调用compute_fine函数
birthday_speed_114 = compute_fine(114, True)
# 如果你不是在生日那天以114km/h的速度驾驶,调用compute_fine函数
non_birthday_speed_114 = compute_fine(114, False)
# 如果你在生日那天以118km/h的速度驾驶,调用compute_fine函数
birthday_speed_118 = compute_fine(118, True)
# 输出上面的示例测试结果
print(f"114 km/h on birthday: ${birthday_speed_114}")
print(f"114 km/h not on birthday: ${non_birthday_speed_114}")
print(f"118 km/h on birthday: ${birthday_speed_118}")
更新日志
1c35a
-于aed17
-于44280
-于e4339
-于47421
-于