-1

Whenever I try to input a string of a name that is first on the list with an index value of 0 (ex. Magnus), it works, however, when I input a different name that has a different index value (ex. Tony) it doesn't work. It seems like the variable 'k' only works on the first item within the list. Is there a way to make it so that the variable 'k' checks the whole list before confirming that there is or isn't any matches? Also, how do you make all the lists and strings lowercase as to make it less case sensitive when searching for matches? It doesn't seem to work when I try to do it.

people_list = [['Magnus', 84], ['Tony', 42], ['Alex', 36], ['James', 29], ['Miles', 51], ['Steve', 13], ['Kenneth', 8], ['Valar', 60]]

find = str(input("Please input a name."))
  for k,v in people_list:
    if find == k:
      print("Name:" + str(k),"& Age:" + str(v))
    elif find != k:
      print("A match was not found.")
      break
    break

1 Answers1

0

The issue is the break statement, as long as you don't find the string you should not break. I keep another variable found that indicates whether. the loop was finished because we found a match or because we finished going over the list.

find = str(input("Please input a name."))
found = False
for k,v in people_list:
    if find == k:
        print("Name:" + str(k),"& Age:" + str(v))
        found = True
        break
if not found:
    print("A match was not found.")
Tom Ron
  • 5,906
  • 3
  • 22
  • 38