Python-Suchwörterbuchschlüssel für die Sucheingabe
Also hier ist meine Frage:
Ich möchte ein Wörterbuch durchsuchen, um festzustellen, ob ein Schlüssel ein vom Benutzer eingegebenes Schlüsselwort enthält. Beispielsweise sucht der Benutzer nach John.
elif option == 3:
count = 0
found = None
search_key = input("What do you want to search for? ").lower()
for key, val in telephone_directory.items(): #takes each element in telephone directory
if search_key in key: #checks if it contains search_key
if found is None:
found = val
count = 1
if found is not None:
print(" ")
print("More than one match found. Please be more specific.")
print(" ")
count = 2
break
if found is None:
print("Sorry, " + str(search_key) + " was not found.")
print(" ")
function_options() #redirects back
if found is not None and count < 2:
print(str(search_key) + " was found in the directory.")
print("Here is the file on " + str(search_key) + ":")
print(str(search_key) + ":" + " " + telephone_directory[search_key])
print(" ")
function_options() #redirects back
Hier bin ich also gerade. Unabhängig von der Suche wird "Wurde nicht gefunden" zurückgegeben, auch wenn es sich um den gesamten Schlüssel handelt. Was mache ich falsch?