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))
添加回答
舉報