0
not working? It goes through each one then print output but not print Not Found unless the input name is not in list at all.
Contact =[('James', 42), ('Amy', 24), ('John', 31), ('Amanda', 63), ('Bob', 18)] a=0 name = input() for x in contacts: If name in x: print(str(x[0]) + " is " + str(x[1]) break elif a < 4: a += 1 else: print("Not Found")
5 odpowiedzi
+ 4
Most of the Python keywords and built-in functions are written in lowercase. It makes a huge difference.
+ 4
the list of tuples (names and ages) is stored with the variable name *contact*, but in the for loop *contacts* is used
btw: the line that outputs the result can be done easier and without converting the ages to a string, if you use a comma to separate the print arguments:
(sample)
...
number = 42
print("size: " + str(number)) # <<< instead of using this
print("size:",number ) # <<< do it like this
+ 1
Two more problems:
1. When you test 'if name in x', remember x is a tuple with name and a number. If a testcase enters an existing number, your code will return the corresponding name, instead of "Not found".
2. The indentation is wrong. The elif and else statements are linked to the for loop, when they should be to the if statement.
0
That not it. It keep printing not found before print the name and age.
0
Test case #5 is the only one that does not have green check mark and it is locked. I am assuming that it should be Not Found as the answer.