python - 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
有什么假设吗?
解决方案
这是因为下面的比较
elif s_country == 'usa' or s_country == 'Usa':
print(f"{s_country.upper()}")
从您传递usa
给s_country
变量的输入
推荐阅读
- java - 如何在此代码中使用 if else 语句而不是 try 和 catch 异常
- angular - 如何对从数组中删除重复项的方法进行单元测试?
- spring-boot - 发送多封邮件时 JTA 超时
- android - 如果我将一个 EditText 分配给另一个 EditText,为什么它们指向相同的值?
- java - java rabbitmq 确认无效
- amazon-web-services - AWS 上的 Dockerized Jenkins 服务器
- java - 使用 element.click() 时 WebDriverException '未知错误:脚本没有返回元素引用'
- python - 我从 AutoEncoder tensorflow2.0 按时间顺序得到错误的数据
- flutter - 如果我使用其控制器将文本设置为文本字段,则 ScrollView 会自动滚动
- react-native - 收到错误“GMAPS 路由请求上的 react-native-maps-directions 错误未知错误。” 建立到坐标的连接