I'm trying to write a code to let user input a letter and return the digit like our phone keypad. My working is:
phone_letters = [" ", "", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"]
def let_to_num():
letter =input("Enter: ")
key = 0
while key < 10:
if letter.upper() in phone_letters[key]:
return key
else:
return "Not found"
print(let_to_num())
However it doesn't work and the correct answer should be:
while key < 10:
if letter.upper() in phone_letters[key]:
return key
else:
key += 1
return "Not found"
What i understand is "IF" input not in phone_letter, then we jump to else and return "Not found". "IF" input can find in phone_letter then return "key" and end.
I don't understand why the (return "Not found") put inside the else statement will return "Not found" for every input? And what is the function for "key += 1"
Aucun commentaire:
Enregistrer un commentaire