create function let_to_num() - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: create function let_to_num() (/thread-8301.html) |
create function let_to_num() - Truman - Feb-13-2018 let_to_num() takes input of a single letter, space or empty string stored in an argument variable: letter use while key < 10: to try numbers 0 - 9 as index for phone_letters ("key" = phone dial pad key) check if letter variable is in the index of phone_letters[key] key = 0 while key < 10: if # Create Code: determine if letter is ** in ** any of the phone_letters[key] where key is the index 0 -9:return key else: key = key + 1 return "Not Found" return the number or "Not Found" call let_to_num() to test the function so it prints the argument and return value with: space lowercase letter different letter, uppercase a number my code: phone_letters = [' ', '', 'ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ'] def let_to_num(letter): letter=input("Enter single letter, space or empty: ") key = 0 while key < 10: for letter in phone_letters: if l in letter: return (phone_letters.index(letter)) else: key = key + 1 else: print("Not found") print(let_to_num(letter))Whatever character that I add it prints 9. I'm not sure that I even understand the task, your opinion is appreciated. RE: create function let_to_num() - ka06059 - Feb-15-2018 lets assume no semantic error.... phone_letters = [' ', '', 'ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ'] def let_to_num(): #letter argument removed, input() will do the job letter=input("Enter single letter, space or empty: ") key = 0 while key < 10: # 'for letter in phone_letters' loop not needed, this line will loop along phone_letters anyway if letter in phone_letters[key]: return key #key is the index itself else: key = key + 1 return ("Not found") #should return 'not found' instead, it will be printed in line 11 print(let_to_num()) check out the comment lines for few explanations
|