+ 1
Can someone tell me what’s wrong?
My code should work by trying to find a name(input) in the list, and output an according message, but it only works for the first name on the list. https://sololearn.com/compiler-playground/ckXXGoBVN3jg/?ref=app
2 Antworten
+ 7
Presently, there is a break statement in both branches of the if/else, so the loop never advances to the next element.
There is an easy fix to make it do exactly what you need. Convert the if/else into a for/else by reducing the indentation of the else clause and removing its break statement.
for name in name_database:
print("Searching...")
if name == search:
print("Found!")
break
else: #<--- unindent
print("Not found :(") #<--- unindent
#break <--- remove
In a for/else statement, the else clause executes if and only if the for loop finishes without encountering a break statement.
+ 7
# Hi, underlineyou !
# You can use this solution as a hint for how to solve the problem more easily.
name_list = ["Jhon" , "Mary" , "Peter" , "Paul", "Louie"]
name = input().title()
print("Searching...\n...\n...")
if name in name_list:
print(f"\t-> {name} found in list!")
else:
print(f"\t-> {name} not found in list :(")