Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

No output from print function

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading