如何修复While语句上的无限“”打印?

2024-06-25 23:15:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图编码这个凯撒密码解码器,但每当输入消息有任何“(空格),它只是打印”无限 [ps:刚刚开始编码]

已经尝试更改-if-以检查meslist[x]是否等于“”,但它似乎是一个“有效”的东西

代码如下:

import string
letras = list(string.ascii_lowercase)
message = input("qual sua menssagem codificada?")
cod = input("qual a numeração da cifra?")
meslist = list(message)

trans = True
num = len(meslist)
x = 0
while trans:

if num > x:
    if meslist[x] in letras:
        a = letras.index(meslist[x])
        print(letras[a + 2])
        x = x + 1
        trans = True
    else:
        print(" ")
        trans = True
elif num == 0:
        trans = False'

只是希望它能以正确的方式打印出来。你知道吗


Tags: true密码message编码transinputstringif
1条回答
网友
1楼 · 发布于 2024-06-25 23:15:36

我修复了如下代码:

while trans:
    if num > x:
        if meslist[x] in letras:
            a = letras.index(meslist[x])
            print(letras[a + 2])
            x = x + 1
            trans = True
        else:
            print(" ")
            trans = True
            # to increase x by 1, to take the next char. in the next loop
            x = x + 1
    # num == x means the end of the loop
    elif num == x:
            trans = False

相关问题 更多 >