猿问

为每个超过最大值的 X 堆叠“点”

我是一个 python 新手,目前正在学习它的基础知识。我遇到过这个任务,我真的很想解决它,这样我就可以了解将来如何做类似的事情。事情是这样的:编写一个函数来检查驱动程序的速度。这个函数应该有一个参数:速度。如果速度低于 70,则应打印“Ok”。否则,每超过限速(70)5公里,应扣一分,并打印扣分总数。例如,如果速度为80,则应打印:“Points: 2”。如果驾驶员得分超过 12 分,该函数应打印:“许可证已暂停”


这是我目前想到的,但无法解决文本的粗体部分。如果您能帮助我,我将不胜感激。谢谢 !


def speed_check(speed):

warning_point = 0

max_speed = 70

if (speed <= max_speed):

    print ("OK")

elif (speed >=130):

    print ("Licence suspended, you total warning points is 12.")

elif ("something must go here"):

    warning_point +=1

    print("Current warning point is {0}".format(warning_point))

速度检查(75)


互换的青春
浏览 198回答 3
3回答

呼啦一阵风

需要一个全局变量来跟踪已授予的警告点数量。下面应该这样做,如果有道理或者有你想要解释的部分,请评论。def speed_check(speed):&nbsp; &nbsp; global warning_point&nbsp; &nbsp; max_speed = 70&nbsp; &nbsp; if speed <= max_speed:&nbsp; &nbsp; &nbsp; &nbsp; print ("OK")&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; warning_point += (speed-max_speed) // 5&nbsp; &nbsp; &nbsp; &nbsp; print("Current warning point is {0}".format(warning_point))&nbsp; &nbsp; if warning_point >= 12:&nbsp; &nbsp; &nbsp; &nbsp; print("Licence suspended, you total warning points is at least 12.")warning_point = 0speed_check(75)speed_check(85)speed_check(115)

汪汪一只猫

您可以将速度限制70和当前速度80除以每个点的数量。然后你可以减去这些来获得积分。import mathdef speed_check(current_speed):&nbsp; &nbsp; max_speed = 70&nbsp; &nbsp; if current_speed <= max_speed:&nbsp; &nbsp; &nbsp; &nbsp; print("OK")&nbsp; &nbsp; elif (current_speed >=130):&nbsp; &nbsp; &nbsp; &nbsp; print ("Licence suspended, you total warning points is 12.")&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; points = (current_speed - max_speed) // 5&nbsp; &nbsp; &nbsp; &nbsp; print(f"Points: {int(points)}")

慕村225694

您可以减去速度限制,除以 5,然后加上 1 偏移量,因为1 / 5 = 0import mathdef speed_check(current_speed):&nbsp; &nbsp; max_speed = 70&nbsp; &nbsp; if current_speed <= max_speed:&nbsp; &nbsp; &nbsp; &nbsp; print("OK")&nbsp; &nbsp; elif (current_speed >=130):&nbsp; &nbsp; &nbsp; &nbsp; print ("Licence suspended, you total warning points is 12.")&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; points = math.floor((current_speed - max_speed) / 5) + 1&nbsp; &nbsp; &nbsp; &nbsp; print("Current warning point is {0}".format(points))
随时随地看视频慕课网APP

相关分类

Python
我要回答