Why is the console showing KeyError:'H'

I am making a program where a letter of a piece of text is replaced by its corresponding number(like a is replaced by 1, b is replaced by 5 and so on). If the letter is uppercase, it is first converted into lowercase.

Here is an example

INPUT

print(alphabet_position(‘hello world’))

OUTPUT

8 5 12 12 15 23 15 18 12 4

HERE IS THE CODE

The output is fine when all the letters are lowercase. But it comes to uppercase letters, the console shows an error.

Here is an error shown by the console when the input is print(alphabet_position(“Hello World”)

Traceback (most recent call last):
  File "C:/Users/admin/Desktop/Code/untitled/sample.,py.py", line 15, in <module>
    print(alphabet_position('Hello World'))
  File "C:/Users/admin/Desktop/Code/untitled/sample.,py.py", line 12, in alphabet_position
    output += " "+ str ( code[N] )
KeyError: 'H'

Process finished with exit code 1


As you see here, I got a KeyError. So, I must have done some something wrong in the dictionary code and maybe somewhere else also. Well, I am not able to find the error so I would like to get help from the community

Every suggestion will be appreciated
THANK YOU

the H is still uppercase, so something went wrong with .lower().

strings are immutable, so .lower() returns a new string which is lowercase. You do nothing with the lowercase string returned by lower method

2 Likes

I don’t get you. Can you give me a better explanation of what to do?

you seem to expect that .lower() will mutate the string, it won’t. It will return a new string in all lowercase. But as we can see here:

text.lower()

you do nothing with the returned result. Maybe you want to store the returned result in a variable?

Hey @stetim94, what @jyotishkaaich is trying to tell you is that Strings are inmutable. You cannot tell Python to just do a text.lower(), without assign it to a new variable.
If you do something like this:

def alphabet_position(text):
    for i in text:
      if i.isupper():
        text = text.lower()

This code will check each letter of your text argument, if it encounter an uppercase letter (method .isupper is used to check), it will do a text.lower() and ASSIGN IT TO A NEW VARIABLE which name is text. Yeah, it has the same name but ITS A NEW VARIABLE.
Your new text, which is all lowercase will be passed as argument for w/e it happens next inside the function.
You can even do it simpler:

def alphabet_position(text):
    text = text.lower()

This line will do a text.lower() and assign the result to a NEW VARIABLE called text, no matter if the text you input is all lowercase or not.
In both cases, the function will work WITH A NEW VARIABLE which is a .lower() version of the original text.
The most important to notice is that strings are inmutable. the lowercase text of both versions described above, is a new variable that replaced the old one. In the first case, it will replace the original text only if an uppercase is found. The second method will always call the .lower() method, apply it to the text and assign it to a new var which has the same name “text”.
I hope this helps.

1 Like