在Python中搜索数组以进行精确匹配

时间:2019-12-08 01:30:49

标签: python

我想知道如何对精确匹配进行数组搜索,如果找不到该城市,它应该输出“不是密歇根州的城市”。

代码应放在: 芝加哥 布鲁克林区 Watervliet Acme

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. 

1 个答案:

答案 0 :(得分:0)

您可以使用in语句。

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

请注意,in语句是完全匹配的。这意味着您可能需要修剪和小写/大写您的输入。