Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my code returning a second "matches None" line?

I have to create a function in Python that matches keypad letters with their corresponding number(excluding 1 & 0).
When I run my code and input a letter, I get the correct match, but I also get a (letter,"matches",None) line below it. What am I doing wrong and what do I need to fix to make sure that the None line doesn’t occur?
Code:

def keypad(ch):
    if ch == "A" or ch == "a" or ch == "B" or ch == "b" or ch == "C" or ch == "c":
        print(ch,"matches",2)
    elif ch == "D" or ch == "d" or ch == "E" or ch == "e" or ch == "F" or ch == "f":
        print(ch,"matches",3)

s = str(input("Enter a letter:"))
print(s,"matches",keypad(s))

Sample output for this:

Enter a letter:D
D matches 3
D matches None

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your keypad function calls print, which is the first output line you see. Since its doesn’t explicitly return anything, it implicitly returns None, which you then print in the last line of code.

In short, remove the print from your last call:

s = str(input("Enter a letter:"))
keypad(s)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading