[De-massive elif] Smart multi-branch by utilizing dictionary type

Python does not have a switch statement. Therefore, when implementing a multi-branch, you need to write an if statement and multiple elif statements. "Isn't it something I can write better ..." I thought so and decided to write as follows. smart! (Self-praise)

How do you guys write? We look forward to your comments!

Before

Prefectures= "Aichi prefecture"

if prefecture== "Niigata Prefecture":
Prefectural office location= "Niigata City"
elif prefecture== "Toyama Prefecture":
Prefectural office location= "Toyama City"
elif prefecture== "Ishikawa Prefecture":
Prefectural office location= "Kanazawa"
elif prefecture== "Fukui prefecture":
Prefectural office location= "Fukui City"
elif prefecture== "Gifu Prefecture":
Prefectural office location= "Gifu City"
elif prefecture== "Nagano Prefecture":
Prefectural office location= "Nagano city"
elif prefecture== "Yamanashi Prefecture":
Prefectural office location= "Kofu City"
elif prefecture== "Shizuoka Prefecture":
Prefectural office location= "Shizuoka City"
elif prefecture== "Aichi prefecture":
Prefectural office location= "Nagoya city"

print(Prefectures+ "The prefectural capital of" +Prefectural office location+ "is.")

After

Prefectures= "Aichi prefecture"

Prefecture to prefectural office location= {"Niigata Prefecture":"Niigata City", "Toyama Prefecture":"Toyama City", "Ishikawa Prefecture":"Kanazawa", "Fukui prefecture":"Fukui City", "Gifu Prefecture":"Gifu City", "Nagano Prefecture":"Nagano city", "Yamanashi Prefecture":"Kofu City", "Shizuoka Prefecture":"Shizuoka City", "Aichi prefecture":"Nagoya city"}

print(Prefectures+ "The prefectural capital of" +Prefecture to prefectural office location[Prefectures] + "is.")

Recommended Posts

[De-massive elif] Smart multi-branch by utilizing dictionary type
Dictionary type 2
Dictionary type 1