Python I';我有一个正常工作的布尔函数,但我的代码非常笨拙。我怎样才能使它更平滑?
问题如下:Python I';我有一个正常工作的布尔函数,但我的代码非常笨拙。我怎样才能使它更平滑?,python,python-3.x,function,boolean,logic,Python,Python 3.x,Function,Boolean,Logic,问题如下: def caught_speeding(speed, is_birthday): if is_birthday: if speed <= 65: return 0 if 66 <= speed <= 86: return 1 if speed >= 86: return 2 if not is_birthday: if speed <= 60: return 0
def caught_speeding(speed, is_birthday):
if is_birthday:
if speed <= 65:
return 0
if 66 <= speed <= 86:
return 1
if speed >= 86:
return 2
if not is_birthday:
if speed <= 60:
return 0
if 61 <= speed <= 81:
return 1
if speed >= 81:
return 2
你开得有点太快了,一个警察拦住了你。
编写代码以计算结果,编码为int值:0=否
票,1=小票,2=大票。如果速度小于等于60,则
结果是0。如果speed
介于61和80之间,则结果为1。
如果speed
等于或大于81,则结果为2。除非它是你的生日
--
在那一天,你的速度可以在任何情况下提高5
我的功能如下:
def caught_speeding(speed, is_birthday):
if is_birthday:
if speed <= 65:
return 0
if 66 <= speed <= 86:
return 1
if speed >= 86:
return 2
if not is_birthday:
if speed <= 60:
return 0
if 61 <= speed <= 81:
return 1
if speed >= 81:
return 2
def超速(速度,是生日):
如果是你的生日:
如果速度这将是我的第一次尝试。如果不是生日,您可以在每隔一个分支返回后删除
def超速(速度:int,is\u生日:bool)->int:
如果是你的生日:
如果速度不是笨拙的话。我想您可以通过说if is_birth:speed-=5
来稍微减小大小,然后使用非生日序列。您可以停止检查不必要的条件:例如,在确定速度大于65后,您不需要在下一个if语句中检查它。欢迎使用StackOverflow。我认为这更适合你的问题。我投票结束这个问题,因为它可以进一步简化-elif 66