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

时间:2019-08-26 03:17:18

标签: python

im试图对这个凯撒密码解码器进行编码,但是只要输入消息中有任何空格,它都会无限打印 [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'

只是期望它能以正确的方式打印。

1 个答案:

答案 0 :(得分: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
            # 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