首页 > 解决方案 > 为什么在 python 中创建一个函数时出现语法错误,该函数接受两个参数来确定我将获得的超速罚单类型?

问题描述

你开得太快了,警察拦住了你。编写一个函数以返回 3 种可能结果之一:“无票”、“小票”或“大票”。如果您的速度为 60 或更低,则结果为“无票”。如果速度介于 61 和 80 之间(含),则结果为“小票”。如果速度为 81 或更高,则结果为“大票”。除非是你的生日——在你生日那天,你的速度在所有情况下都可以提高 5 倍。

这是我为解决上述问题而创建的函数(在 Jupyter Notebook 中):

    def caught_speeding (speed, is_birthday):
        if speed <= 60:
            print ('no ticket')
        elif speed >= 66 and speed <= 85 and is_birthday = 'Y':
            print ('small ticket')
        elif speed >= 61 and speed <= 80 and is_birthday = 'N': 
            print ('small ticket')
        elif speed >= 86 and is_birthday = 'Y':
            print ('big ticket')
        elif speed >= 81 and is_birthday = 'N':
            print ('big ticket')
        else:
            print ('null')

不幸的是,我收到了这个错误:

[错误截图[1]

如果我只使用第一个参数创建函数,我不会收到此错误。

标签: python

解决方案


尝试==平等比较,而不是=

请参阅有关比较的文档 https://docs.python.org/3/library/stdtypes.html


推荐阅读