将函数的输出保存到列表中的Python函数

问题描述 投票:0回答:1

该程序应向用户提供输入,以便该程序接受该输入,并在用户输入“保存”时将加密的文本保存在列表中,以释放输出加密的文本。这里的问题是,当用户输入保存时,它只会将加密的'save'单词附加到列表中。

alph = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
        'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']


def encrypt(text):
    global semi_crypt
    semi_crypt = ""

    for char in text:
        if char not in alph:
            semi_crypt = semi_crypt + char
        try:
            if char in alph:
                x = alph.index(char)
                semi_crypt = semi_crypt + alph[x + 3]
        except IndexError:
            if char == "x":
                semi_crypt = semi_crypt + 'a'
            elif char == "y":
                semi_crypt = semi_crypt + 'b'
            elif char == 'z':
                semi_crypt = semi_crypt + "c"
    return semi_crypt


print('''
This is Encrypto
''')
print('Type end to save when you are done')

while True:
    crypted_l = []
    message = input('Encrypt something: ')
    print(encrypt(message))
    crypted_l.append(encrypt(message))
    if message == 'save':
        print(f"{'Saved:'}{crypted_l}")
        break
python python-3.x function encryption global-variables
1个回答
0
投票
crypted_l移出循环,如下:

crypted_l = [] while True: message = input('Encrypt something: ') print(encrypt(message)) crypted_l.append(encrypt(message)) if message == 'save': print(f"{'Saved:'}{crypted_l}") break

© www.soinside.com 2019 - 2024. All rights reserved.