'Python Return a Variable in the file
I have tuple variables which are France, Germany. I'm trying to give a value to my bring_cities function and if it's France or Germany, I like to see the France and Germany tuple objects. Is there any shortcut to not use if loops like I did in the below ?
France = (
('Paris', 'Paris'),
('Lyon', 'Lyon'),
)
Germany = (
('Frankfurt', 'Frankfurt'),
('Berlin', 'Berlin'),
)
cities = (('', ''),) + France + Germany
def bring_cities(country):
if country == 'France':
return France
if country == 'Germany':
return Germany
...
Solution 1:[1]
Resuming gerda's answer
France = (
('Paris', 'Paris'),
('Lyon', 'Lyon'),
)
Germany = (
('Frankfurt', 'Frankfurt'),
('Berlin', 'Berlin'),
)
dictionary = {"France": France, "Germany": Germany}
def bring_cities(country):
print(dictionary[country])
user_choice = input("Enter a Country (France/Germany) and we will give you Cities in it: ")
bring_cities(user_choice)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
