Python Forum
create function let_to_num()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create function let_to_num()
#1
In my assignment I have to convert a number to letter using a list. I've been able to do this except I'm stuck on the bonus question. When the input is left blank, the index needs to refer to [2]. I've tried to add an elif statement, but I continue to get the first index no matter what I do. The course has not covered any information on handling blank input entries. My code is as follows:
phone_letters = [' ', '','ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ']
letter = input('Enter a single letter, space or empty: ')

def let_to_num(letter):
    key = 0
    while key < 10:
        if letter.upper() in phone_letters[key]:
            return key
        elif letter == False: #don't know how to deal with a blank input
            key = 1
        else:
            key = key + 1           
    return "Not Found"

print(letter,"=",let_to_num(letter))
Reply
#2
The input isn't your problem, it's how you're checking. You probably want to just put a special case of checking for your input being "" at the beginning of the function.

The problem is that you're seeing if the letter is located in the values of phone_letters, but an empty string will always match that test. So your check looks for it in key=0 and "finds" it.

>>> "x" in "foobar"  # not present
False
>>> "" in "foobar"   # empty string always present
True
So you need to check for input == "" early and deal with that. Then do your loop (or whatever).

if letter == "":
    return 2
.....

Note that this is an excellent use for a dictionary. If you create a dictionary with the number of each of the letters you want, then looking it up and returning that value is easy. It also handles the empty string without a special case.

>>> let_to_num = {"": 2,
... " ": 1,
... "A":3,
... "B":3,
... "C":3,
... "D":4}
>>> let_to_num.get("", "Not Found")  # empty string
2
>>> let_to_num.get(" ", "Not Found")  # space
1
>>> let_to_num.get("D", "Not Found")  # regular letter
4
>>> let_to_num.get("$", "Not Found")  # invalid letter
'Not Found'
Reply
#3
Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug New to coding, Using the zip() function to create Diret and getting weird results Shagamatula 6 1,438 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  python create function validation mg24 1 832 Nov-15-2022, 01:57 AM
Last Post: deanhystad
  create my exception to my function korenron 2 787 Nov-09-2022, 01:50 PM
Last Post: korenron
  Create a function for writing to SQL data to csv mg24 4 1,151 Oct-01-2022, 04:30 AM
Last Post: mg24
  Create SQL connection function and validate mg24 1 937 Sep-30-2022, 07:45 PM
Last Post: deanhystad
  How to define a function to create a resorted list? sparkt 6 2,813 Aug-08-2020, 04:10 PM
Last Post: sparkt
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,406 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Tried to create a function in a Datacamp course - why a is not equal to x_copy? danlin123 1 1,735 Jun-21-2020, 09:40 PM
Last Post: jefsummers
  Create function around sql ayomayam 1 1,591 Feb-05-2020, 07:20 PM
Last Post: ayomayam
  Create a function to find words of certain length ag4g 2 4,067 Apr-21-2019, 06:20 PM
Last Post: BillMcEnaney

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020