3 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊
需要一個(gè)全局變量來跟蹤已授予的警告點(diǎn)數(shù)量。下面應(yīng)該這樣做,如果有道理或者有你想要解釋的部分,請?jiān)u論。
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貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以將速度限制70和當(dāng)前速度80除以每個(gè)點(diǎn)的數(shù)量。然后你可以減去這些來獲得積分。
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貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以減去速度限制,除以 5,然后加上 1 偏移量,因?yàn)? / 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))
添加回答
舉報(bào)