我有太多的if语句,我很难理解字典系统。你知道吗
这看起来很容易解决,但我做的每一件事都会让事情变得更糟。你知道吗
"""
Find the place where the person was born.
Possible locations are following: Kuressaare, Tartu, Tallinn, Kohtla-Järve, Narva, Pärnu,
Paide, Rakvere, Valga, Viljandi, Võru and undefined. Lastly if the number is incorrect the function must return
the following 'Wrong input!'
:param birth_number: int
:return: str
"""
return
if not is_valid_birth_number(birth_number):
return "Wrong input!"
if 10 >= birth_number >= 1:
return "Kuressaare"
elif 20 >= birth_number >= 11:
return "Tartu"
elif 220 >= birth_number >= 21:
return "Tallinn"
elif 270 >= birth_number >= 221:
return "Kohtla-Järve"
elif 370 >= birth_number >= 271:
return "Tartu"
elif 420 >= birth_number >= 371:
return "Narva"
elif 470 >= birth_number >= 421:
return "Pärnu"
elif 490 >= birth_number >= 471:
return "Tallinn"
elif 520 >= birth_number >= 491:
return "Paide"
elif 570 >= birth_number >= 521:
return "Rakvere"
elif 600 >= birth_number >= 571:
return "Valga"
elif 650 >= birth_number >= 601:
return "Viljandi"
elif 710 >= birth_number >= 651:
return "Võru"
elif 999 >= birth_number >= 711:
return "undefined"
需要消除idcode.py:149:1: C901 'get_birth_place' is too complex (16)
错误。你知道吗
以^{} 为例。你知道吗
我更喜欢像@Barmar那样把数据放在一起。这导致:
我会使用字典,它包含键作为范围,值是位置的名称。 然后遍历它,检查给定的
birth_number
是否在键中。你知道吗基本上:
我认为这是一个很清楚的处理方法。你知道吗
使用将每个范围的结尾映射到返回值的列表。你知道吗
你不需要每个范围的开始和结束,因为它们是有序的。仅仅是范围的尽头就足够了。你知道吗
相关问题 更多 >
编程相关推荐