没有从打印函数中输出

huangapple go评论63阅读模式
英文:

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(&quot;Enter string with 1 character: &quot;)
letter_map=&quot;aeostr&quot;
cipher=&quot;rtsoea&quot;
cnt=0

while cnt &lt; 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(&quot;Enter string with 1 character: &quot;)
letter_map=&quot;aeostr&quot;
cipher=&quot;rtsoea&quot;

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(&quot;Enter string with 1 character: &quot;):
    secret = secret[0] # ensure only one character
    letter_map = &quot;aeostr&quot;
    cipher = &quot;rtsoea&quot;

    if (i := letter_map.find(secret)) &gt;= 0:
        print(cipher[i])
    elif (i := letter_map.upper().find(secret)) &gt;= 0:
        print(cipher.upper()[i])
else:
    print(&#39;Invalid input&#39;)

Or...

if secret := input(&quot;Enter string with 1 character: &quot;):
    letter_map = &quot;aeostr&quot;
    letter_map += letter_map.upper()
    cipher = &quot;rtsoea&quot;
    cipher += cipher.upper()
    if (i := letter_map.find(secret[0])) &gt;= 0:
        print(cipher[i])
else:
    print(&#39;Invalid input&#39;)

huangapple
  • 本文由 发表于 2023年2月24日 00:00:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定