Home > Enterprise >  for loop with while loop wont break
for loop with while loop wont break

Time:11-02

# Initialized list of cities
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.
for i in range(len(CitiesInMichigan)):
    if CitiesInMichigan[i] == inCity:
        while CitiesInMichigan is True:
            print("City found. ")
            break
        else:
            print("Not a city in Michigan. ")
            input("Enter name of city: ")



# If the city is found, print "City found."

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

So what I am trying to go for was to have the input case insensitive so (Acme, ACME, acMe, etc.) would work and to have it break if input matches or try again if input is false. Instead I have been receiving that all inputs are not a city of Michigan. What am I doing wrong?

P.S I am studying python for school and as a passion, I am still new and wrapping my head around it please criticize anything I do wrong to further improve my codings. Thank you

CodePudding user response:

Your while loop is unnecessary and your else clause should simply be moved outside of the loop. You also need to loop all the way back to your initial input if a city is not found.

cityFound = False
while not cityFound:
  inCity = input("Enter name of city: ")
  for i in range(len(CitiesInMichigan)):
    if CitiesInMichigan[i] == inCity:
      print("City found. ")
      cityFound = True
      break
  if not cityFound:
    print("Not a city in Michigan. ")

To do case insensitive matching you should use the string upper method on both the input and the value in your CitiesInMichigan list before you check for equality.

Other notes:

  • You don't need to use an index in your for loop, you can just use for city in CitiesInMichigan
  • Generally variable names begin with lower case letters (citiesInMichigan instead of CitiesInMichigan). Class names start with upper case letters

CodePudding user response:

as others have mentioned, the while loop is unnecessary. below code is longer than it needs to be, but it explains itself well.

# Initialized list of cities
CitiesInMichigan = ["Acme", "Albion", "Detroit", "Watervliet", "Coloma", "Saginaw", "Richland", "Glenn", "Midland",
                    "Brooklyn"]

def get_input():
    '''Get user input and capitalize it'''
    inCity = input("Enter name of city: ")
    inCity = inCity.capitalize()
    check_against_list(name=inCity)

def check_against_list(name):
    '''check if the item is in the list. if yes, print. if not, print and start over'''
    if name in CitiesInMichigan:
        print("City found. ")
    else:
        print("Not a city in Michigan. ")
        get_input()

get_input()

CodePudding user response:

CitiesInMichigan = ["Acme", "Albion", "Detroit", "Watervliet", "Coloma", "Saginaw", "Richland", "Glenn", "Midland",
                 "Brooklyn"]
CitiesInMichigan_casefold = [i.casefold()  for i in CitiesInMichigan ]
inCity = input("Enter name of city: ")
while True:
    if inCity.casefold() in CitiesInMichigan_casefold:
        print("city found",inCity)
        break
    else:
        print("city not found")
        inCity = input("Enter name of city again: ")

Try this. you have to compare case fold strings both in input and as well as from the mentioned list.
if the city is found, it prints city name and breaks the loop.
else it continues until the city is matched.
  • Related