首页 > 解决方案 > def 函数中的额外元素

问题描述

所以,这是我的代码:

def city_country(f_city, f_country, s_city, s_country, t_city, t_country):
    print(f"{f_city.title()}, {f_country.title()}")
    print(f"{s_city.title()}, {s_country.upper()}")
    print(f"{t_city.title()}, {t_country.title()}")
    if f_country == 'usa' or f_country == 'Usa':
        print(f"{f_country.upper()}")
    elif s_country == 'usa' or s_country == 'Usa':
        print(f"{s_country.upper()}")
    elif t_country == 'usa' or t_country == 'Usa':
        print(f"{t_country.upper()}")
    return 
city_country('moscow', 'russia', 'washington', 'usa', 'berlin', 'germany')

一切都会好的,但由于某种原因,我在函数输出的末尾显示了一个额外的元素:

Moscow, Russia
Washington, USA
Berlin, Germany
USA

有什么假设吗?

标签: python

解决方案


这是因为下面的比较

elif s_country == 'usa' or s_country == 'Usa':
      print(f"{s_country.upper()}")

从您传递usas_country变量的输入


推荐阅读