Python 返回文件中的变量

发布于 2025-01-09 00:02:28 字数 436 浏览 1 评论 0原文

我有法国、德国的元组变量。我试图为我的 Bring_cities 函数赋予一个值,如果是法国或德国,我喜欢查看法国和德国元组对象。有没有像我在下面所做的那样不使用 if 循环的快捷方式?

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
    ...

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
    ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦笑流年记忆 2025-01-16 00:02:28

您可以创建一个字典,这样就不必为每个国家/地区编写 if 语句。您可以使用以下代码来实现此目的。

France = (
    ('Paris', 'Paris'),
    ('Lyon', 'Lyon'),
)
Germany = (
    ('Frankfurt', 'Frankfurt'),
    ('Berlin', 'Berlin'),
)

dictionary = {"France": France, "Germany": Germany}


def bring_cities(country):
    return dictionary[country]

为了使其更短,您可以在字典中定义您的国家/地区。

dictionary = {
              "France": (('Paris', 'Paris'), ('Lyon', 'Lyon')),
              "Germany": (('Frankfurt', 'Frankfurt'), ('Berlin', 'Berlin'),)
              }

You can create a dictionary so you don't have to write an if statement for each country. You can use the following code for that.

France = (
    ('Paris', 'Paris'),
    ('Lyon', 'Lyon'),
)
Germany = (
    ('Frankfurt', 'Frankfurt'),
    ('Berlin', 'Berlin'),
)

dictionary = {"France": France, "Germany": Germany}


def bring_cities(country):
    return dictionary[country]

to make it even shorter you can define your Countries inside the dictionary.

dictionary = {
              "France": (('Paris', 'Paris'), ('Lyon', 'Lyon')),
              "Germany": (('Frankfurt', 'Frankfurt'), ('Berlin', 'Berlin'),)
              }
勿忘初心 2025-01-16 00:02:28

恢复格尔达的回答

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文