在Python中搜索完全匹配的数组

2024-09-27 00:21:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道我如何能使一个精确匹配的数组搜索,如果城市没有找到它应该输出“不是一个城市在密歇根州。”

代码应该输出: 芝加哥 布鲁克林 沃特夫利特 顶点

citiesInMichigan = ["Acme", "Albion", "Detroit", "Watervliet", "Coloma", "Saginaw", "Richland", "Glenn", "Midland", "Brooklyn"] 

# Get user input
inCity = input("Enter name of city: ")


# Write your test statement here to see if there is a match.
if inCity == citiesInMichigan:
    print("City Found")
# If the city is found, print "City found."

# Otherwise, "Not a city in Michigan" message should be printed. 

Tags: 代码cityinputifis数组acmeprint
1条回答
网友
1楼 · 发布于 2024-09-27 00:21:53

您可以使用in语句。你知道吗

if inCity in citiesInMichigan:
    print("City Found")
else:
    print("Not a city in michigan.")

注意in语句是完全匹配的。这意味着您可能需要对输入进行微调和小写/大写。你知道吗

相关问题 更多 >

    热门问题