英文:
No output from print function
问题
当我尝试使用大写字母A或字母映射内的其他字符串来执行此行代码时,我收到一个空白输出。
else:
    upper_ver = str.upper(cipher[cnt])
    print(upper_ver)
我最初尝试如下:
else:
    print(str.upper(cipher[cnt]))
我不确定我哪里出错了,但我遇到了问题。
英文:
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.
答案1
得分: 0
你应该在第一个if语句中使用 secret.lower() == letter_map[cnt]。
secret = input("输入一个字符:")
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
英文:
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
答案2
得分: 0
secret=input("输入包含一个字符的字符串:")
letter_map="aeostr"
cipher="rtsoea"
for cnt in range(6):
    if secret.lower() == letter_map[cnt]:
        if secret.islower():
            print(cipher[cnt])
        else:
            upper_ver = cipher[cnt].upper()
            print(upper_ver)
英文:
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.
答案3
得分: 0
你根本不需要循环(除非我误解了功能)。这当然只是一个问题...
if secret := input("输入一个字符: "):
    secret = secret[0] # 确保只有一个字符
    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('无效输入')
或者...
if secret := input("输入一个字符: "):
    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('无效输入')
英文:
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论