使用 Python 中的用户输入将变量与数组匹配
Matching variables to an array using user input in Python
我不明白为什么没有更改步骤以遍历循环并检查每个步骤。
# MichiganCities.py - This program prints a message for invalid cities in
Michigan.
# Input: Interactive
# Output: Error message or nothing
# Initialized list of cities
citiesInMichigan = ["Acme", "Albion", "Detroit", "Watervliet", "Coloma",
"Saginaw", "Richland", "Glenn", "Midland", "Brooklyn"]
inCity = "userinput"
step = 0
size = 10
while step < size:
inCity = input("Enter name of city: ") # Get user input
print (inCity)
if inCity == citiesInMichigan[step]: # If the city is found, print "City found."
print ("City found.")
step = step +1
else:
print ("Not a city in Michigan.") # Otherwise, "Not a city in
Michigan"
message should be printed.
citiesInMichigan = ["Acme", "Albion", "Detroit", "Watervliet", "Coloma",
"Saginaw", "Richland", "Glenn", "Midland", "Brooklyn"]
inCity = "userinput"
step = 0
size = 10
while step < size:
inCity = input("Enter name of city: ") # Get user input
print (inCity)
if inCity in citiesInMichigan: # If the city is found, print "City found."
print ("City found.")
step = step +1
else:
print ("Not a city in Michigan.") # Otherwise, "Not a city in Michigan" message should be printed.
嗨!请试试这个。希望它有效
我不明白为什么没有更改步骤以遍历循环并检查每个步骤。
# MichiganCities.py - This program prints a message for invalid cities in
Michigan.
# Input: Interactive
# Output: Error message or nothing
# Initialized list of cities
citiesInMichigan = ["Acme", "Albion", "Detroit", "Watervliet", "Coloma",
"Saginaw", "Richland", "Glenn", "Midland", "Brooklyn"]
inCity = "userinput"
step = 0
size = 10
while step < size:
inCity = input("Enter name of city: ") # Get user input
print (inCity)
if inCity == citiesInMichigan[step]: # If the city is found, print "City found."
print ("City found.")
step = step +1
else:
print ("Not a city in Michigan.") # Otherwise, "Not a city in
Michigan"
message should be printed.
citiesInMichigan = ["Acme", "Albion", "Detroit", "Watervliet", "Coloma",
"Saginaw", "Richland", "Glenn", "Midland", "Brooklyn"]
inCity = "userinput"
step = 0
size = 10
while step < size:
inCity = input("Enter name of city: ") # Get user input
print (inCity)
if inCity in citiesInMichigan: # If the city is found, print "City found."
print ("City found.")
step = step +1
else:
print ("Not a city in Michigan.") # Otherwise, "Not a city in Michigan" message should be printed.
嗨!请试试这个。希望它有效