No output from print function

Question:

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.

Asked By: Aaron Talamantes

||

Answers:

You should use secret.lower() == letter_map[cnt] in first if statement.

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

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

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.

Answered By: UndoneStudios

You don’t need a loop at all (unless I’m misunderstanding the functionality). Surely it’s just a matter of…

if secret := input("Enter string with 1 character: "):
    secret = secret[0] # ensure only one character
    letter_map = "aeostr"
    cipher = "rtsoea"

    if (i := letter_map.find(secret)) >= 0:
        print(cipher[i])
    elif (i := letter_map.upper().find(secret)) >= 0:
        print(cipher.upper()[i])
else:
    print('Invalid input')

Or…

if secret := input("Enter string with 1 character: "):
    letter_map = "aeostr"
    letter_map += letter_map.upper()
    cipher = "rtsoea"
    cipher += cipher.upper()
    if (i := letter_map.find(secret[0])) >= 0:
        print(cipher[i])
else:
    print('Invalid input')
Answered By: Pingu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.