Bottom Page

Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function not returning correct value
So I am an absolute beginner.
And I can't unserstand why a=letter_value(message) makes a a zero.
[icode]def letter_value(letter):
if letter == "A" or letter=="a":
elif letter=="B" or letter=="b":
elif letter=="C" or letter=="c":
elif letter=="D" or letter=="d":
elif letter=="E" or letter=="e":
elif letter=="F" or letter=="f":
elif letter=="G" or letter=="g":
elif letter=="H" or letter=="h":
elif letter=="I" or letter=="i":
elif letter=="J" or letter=="j":
elif letter=="K" or letter=="k":

return l_value

while 0!=1:
message=input("Enter message")
a = letter_value(message)
print(a) [/icode]
Please use the when posting code. The code tag doesn't work here.

The code looks fine. What is your input value?

Here's a quick rewrite of your code with a few small changes.

def letter_value(letter):
    letter = letter.lower()
    if letter == "a":
        l_value = 0
    elif letter == "b":
        l_value = 1
    elif letter == "c":
        l_value = 2
    elif letter == "d":
        l_value = 3
    elif letter == "e":
        l_value = 4
    elif letter == "f":
        l_value = 5
    elif letter == "g":
        l_value = 6
    elif letter == "h":
        l_value = 7
    elif letter == "i":
        l_value = 8
    elif letter == "j":
        l_value = 9
    elif letter == "k":
        l_value = 10
    else: l_value = -1

return l_value

while True:
    message = input("Enter message")
    a = letter_value(message)
The power of Python programming language is that
you can rewrite the function above in two lines of code:

def letter_value(letter):
    return {v: i for i, v in enumerate('abcdefghijk')}.get(letter.lower(), -1)
Or, in this case:

def letter_value(letter):
    return "abcdefghijklmnopqrstuvwxyz".index(letter)
scidam likes this post

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  What is the correct type hint when you want to accept Iterable but not Dictionary LadySvetlana 4 130 Mar-05-2019, 07:33 PM
Last Post: LadySvetlana
  The code seems correct but my files aren't getting deleted taffylim69 1 158 Feb-03-2019, 11:00 AM
Last Post: Larz60+
  problem returning values Naito 7 220 Jan-19-2019, 08:21 PM
Last Post: Naito
  numpynot associted with correct python version ErnestTBass 17 487 Jan-16-2019, 02:47 PM
Last Post: ErnestTBass
  colorama/termcolor not returning coloured lines kapilan15 3 164 Jan-14-2019, 12:57 PM
Last Post: buran
  Correct number wrong position func. albry 5 270 Jan-11-2019, 04:01 PM
Last Post: Larz60+
  Returning true or false in a for loop bbop1232012 3 223 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  LDAP code to query for host not returning data burvil 2 335 Oct-17-2018, 10:03 PM
Last Post: burvil
  Prompting user for number, reading number, squaring it and returning result JHPythonLearner 5 375 Sep-13-2018, 04:05 PM
Last Post: gruntfutuk
  Function not returning expected value Euqinu 4 409 Sep-10-2018, 12:48 PM
Last Post: Euqinu

Forum Jump:

Users browsing this thread: 1 Guest(s)