Bottom Page

Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function not returning correct value
#1
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):
letter=str(letter)
l_value=0
if letter == "A" or letter=="a":
l_value=0
elif letter=="B" or letter=="b":
l_value=1
elif letter=="C" or letter=="c":
l_value=2
elif letter=="D" or letter=="d":
l_value=3
elif letter=="E" or letter=="e":
l_value=4
elif letter=="F" or letter=="f":
l_value=5
elif letter=="G" or letter=="g":
l_value=6
elif letter=="H" or letter=="h":
l_value=7
elif letter=="I" or letter=="i":
l_value=8
elif letter=="J" or letter=="j":
l_value=9
elif letter=="K" or letter=="k":
l_value=10

return l_value

while 0!=1:
message=input("Enter message")
a=0
a = letter_value(message)
print(letter_value(message))
print(a) [/icode]
Quote
#2
Please use the https://python-forum.io/misc.php?action=help&hid=25 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)
    print(letter_value(message))
    print(a)
Quote
#3
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)
Quote
#4
Or, in this case:

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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  problem returning values Naito 7 130 Jan-19-2019, 08:21 PM
Last Post: Naito
  numpynot associted with correct python version ErnestTBass 17 342 Jan-16-2019, 02:47 PM
Last Post: ErnestTBass
  colorama/termcolor not returning coloured lines kapilan15 3 87 Jan-14-2019, 12:57 PM
Last Post: buran
  Correct number wrong position func. albry 5 182 Jan-11-2019, 04:01 PM
Last Post: Larz60+
  Returning true or false in a for loop bbop1232012 3 140 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  LDAP code to query for host not returning data burvil 2 246 Oct-17-2018, 10:03 PM
Last Post: burvil
  Prompting user for number, reading number, squaring it and returning result JHPythonLearner 5 297 Sep-13-2018, 04:05 PM
Last Post: gruntfutuk
  Function not returning expected value Euqinu 4 337 Sep-10-2018, 12:48 PM
Last Post: Euqinu
  I think this is a problem with returning? maby? TheNumericDolfin 8 429 Aug-17-2018, 08:12 PM
Last Post: TheNumericDolfin
  Getting Correct 'a'-tag output soothsayerpg 3 427 Jul-26-2018, 06:25 AM
Last Post: soothsayerpg

Forum Jump:


Users browsing this thread: 1 Guest(s)