3 回答
TA贡献1802条经验 获得超6个赞
需要一个全局变量来跟踪已授予的警告点数量。下面应该这样做,如果有道理或者有你想要解释的部分,请评论。
def speed_check(speed):
global warning_point
max_speed = 70
if speed <= max_speed:
print ("OK")
else:
warning_point += (speed-max_speed) // 5
print("Current warning point is {0}".format(warning_point))
if warning_point >= 12:
print("Licence suspended, you total warning points is at least 12.")
warning_point = 0
speed_check(75)
speed_check(85)
speed_check(115)
TA贡献1898条经验 获得超8个赞
您可以将速度限制70和当前速度80除以每个点的数量。然后你可以减去这些来获得积分。
import math
def speed_check(current_speed):
max_speed = 70
if current_speed <= max_speed:
print("OK")
elif (current_speed >=130):
print ("Licence suspended, you total warning points is 12.")
else:
points = (current_speed - max_speed) // 5
print(f"Points: {int(points)}")
TA贡献1880条经验 获得超4个赞
您可以减去速度限制,除以 5,然后加上 1 偏移量,因为1 / 5 = 0
import math
def speed_check(current_speed):
max_speed = 70
if current_speed <= max_speed:
print("OK")
elif (current_speed >=130):
print ("Licence suspended, you total warning points is 12.")
else:
points = math.floor((current_speed - max_speed) / 5) + 1
print("Current warning point is {0}".format(points))
添加回答
举报