これよりもネストされたif elseステートメントを行うためのよりPython的な方法はありますか?
def convert_what(numeral_sys_1, numeral_sys_2):
if numeral_sys_1 == numeral_sys_2:
return 0
elif numeral_sys_1 == "Hexadecimal":
if numeral_sys_2 == "Decimal":
return 1
elif numeral_sys_2 == "Binary":
return 2
elif numeral_sys_1 == "Decimal":
if numeral_sys_2 == "Hexadecimal":
return 4
elif numeral_sys_2 == "Binary":
return 6
elif numeral_sys_1 == "Binary":
if numeral_sys_2 == "Hexadecimal":
return 5
elif numeral_sys_2 == "Decimal":
return 3
else:
return 0
このスクリプトは、単純なコンバーターの一部です。
and
最上位のif-elseステートメントの条件に移動できます。そうすれば、少なくとも読みやすくなります。残念ながら、Pythonにはswitchステートメントがありません。