亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為每個超過最大值的 X 堆疊“點”

為每個超過最大值的 X 堆疊“點”

互換的青春 2023-06-27 13:47:12
我是一個 python 新手,目前正在學習它的基礎知識。我遇到過這個任務,我真的很想解決它,這樣我就可以了解將來如何做類似的事情。事情是這樣的:編寫一個函數來檢查驅動程序的速度。這個函數應該有一個參數:速度。如果速度低于 70,則應打印“Ok”。否則,每超過限速(70)5公里,應扣一分,并打印扣分總數。例如,如果速度為80,則應打印:“Points: 2”。如果駕駛員得分超過 12 分,該函數應打?。骸霸S可證已暫?!边@是我目前想到的,但無法解決文本的粗體部分。如果您能幫助我,我將不勝感激。謝謝 !def speed_check(speed):warning_point = 0max_speed = 70if (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)
查看完整描述

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)


查看完整回答
反對 回復 2023-06-27
?
汪汪一只貓

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)}")


查看完整回答
反對 回復 2023-06-27
?
慕村225694

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))


查看完整回答
反對 回復 2023-06-27
  • 3 回答
  • 0 關注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號