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
  API call returning list value of 'None' jimbone30 5 162 Jun-14-2019, 07:42 PM
Last Post: jimbone30
  Mysql returning number of rows not data AkaAndrew123 4 296 Jun-10-2019, 02:31 PM
Last Post: AkaAndrew123
  Help with finding correct topic in Python learning yahya01 1 224 Jun-06-2019, 05:01 PM
Last Post: buran
  A question about subprocess taking input from command line and returning output! Aurimas 8 212 May-15-2019, 04:02 PM
Last Post: Aurimas
  Return in correct json format UtiliseIT 3 257 May-13-2019, 11:24 AM
Last Post: snippsat
  How to continue in loop until correct input received sunnyarora 10 355 May-04-2019, 02:37 PM
Last Post: Yoriz
  AssertionError: View function mapping is overwriting an existing endpoint function Zhavi221 7 408 Apr-17-2019, 01:07 PM
Last Post: Zhavi221
  What is the correct type hint when you want to accept Iterable but not Dictionary LadySvetlana 4 210 Mar-05-2019, 07:33 PM
Last Post: LadySvetlana
  The code seems correct but my files aren't getting deleted taffylim69 1 260 Feb-03-2019, 11:00 AM
Last Post: Larz60+
  problem returning values Naito 7 304 Jan-19-2019, 08:21 PM
Last Post: Naito

Forum Jump:


Users browsing this thread: 1 Guest(s)