No output from print function

Advertisements

I am trying to create a program where a letter (in order), a,e,o,s,t, or r, is input and another letter, r,t,s,o,e, or a, is output. For example, if I were to enter a, I would receive r. I am also trying to make this case sensitive, so that if I were to input A, I would get R.

secret=input("Enter string with 1 character: ")
letter_map="aeostr"
cipher="rtsoea"
cnt=0


while cnt < 6:
    if secret == letter_map[cnt]:
        if str.islower(secret):
            print(cipher[cnt])
        else:
            upper_ver = str.upper(cipher[cnt])
            print(upper_ver)
    cnt += 1

When I try to execute this line of code with an uppercase A or other string within the letter map

else:
     upper_ver = str.upper(cipher[cnt])
     print(upper_ver)

I receive a blank output. I originally tried it as

else:
     print(str.upper(cipher[cnt]))

I am not sure where I went wrong, but I am coming up short.

>Solution :

Here’s a better version:

secret=input("Enter string with 1 character: ")
letter_map="aeostr"
cipher="rtsoea"

for cnt in range(6):
    if secret == letter_map[cnt] or secret == letter_map[cnt].upper():
        if str.islower(secret):
            print(cipher[cnt])
        else:
            upper_ver = str.upper(cipher[cnt])
            print(upper_ver)

As Abdul Aziz pointed out, the issue is with the first if. letter_map only contains lowercase letters, and in Python, uppercase doesn’t match lowecase; this can be a source of frustration to new Pythoners coming from languages where lowercase does match uppercase. I also changed the while loop as it makes a little better sense among intermediate Python programmers.

Leave a ReplyCancel reply