多语言展示
当前在线:1077今日阅读:27今日分享:41

如何用PYTHON编写是否超速程序

如何用PYTHON编写是否超速程序
工具/原料

PYTHON

方法/步骤
1

打开JUPYTER NOTEBOOK,新建一个PY文档。

2

#caught_speeding(60, False) → 0#caught_speeding(65, False) → 1#caught_speeding(65, True) → 0#如果是生日的话可以允许时速加5,只是打个比方。这里定义一下规则。

3

def caught_speeding(speed, is_birthday):    if is_birthday:        if speed <= 65:            return 0        elif speed >= 66 and speed <=85:            return 1        elif speed >= 86:            return 2          if not is_birthday:        if speed <= 60:            return 0        elif speed >= 61 and speed <=80:            return 1        elif speed >= 81:            return 2        print(caught_speeding(60, False))print(caught_speeding(65, False))print(caught_speeding(65, True))这里是详细的步骤。

4

def caught_speeding(speed, is_birthday):    more = 0    if is_birthday:        more == 5    if not is_birthday:        more != 5    if speed <= 60 + more:        return 0    elif speed >= 81 + more:        return 2    else:        return 1print(caught_speeding(60, False))print(caught_speeding(65, False))print(caught_speeding(65, True))这是错误的示范。因为如果输入的数字超过范围就会跳过区域。

5

def caught_speeding(speed, is_birthday):    more = 0    if is_birthday:        more == 5    if speed <= 60 + more:        return 0    elif speed >= 81 + more:        return 2    else:        return 1print(caught_speeding(60, False))print(caught_speeding(65, False))print(caught_speeding(65, True))这也是另一个错误的示范。

6

def caught_speeding(speed, is_birthday):    more = 5    if is_birthday:        if speed <= (60 + more):            return 0        elif speed >= (61 + more) and speed <= (80 +more):            return 1        elif speed >= (81 + more):            return 2          if not is_birthday:        if speed <= 60:            return 0        elif speed >= 61 and speed <=80:            return 1        elif speed >= 81:            return 2        print(caught_speeding(60, False))print(caught_speeding(65, False))print(caught_speeding(65, True))如果可以增加速度,那么可以额外定义变量。

注意事项

注意两种方法的区别

推荐信息