gpt4 book ai didi

python - 几乎相同的功能,但不起作用?

转载 作者:太空宇宙 更新时间:2023-11-03 14:42:27 25 4
gpt4 key购买 nike

我尝试了一些使用 python 的东西,虽然 encrypt() 函数工作正常,但 decrypt() 函数没有给我任何输出,甚至没有错误:(

我的代码:

import os
abc=['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', ' ', '.', ',', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '+', '-', ':', "'"]
mixed=abc[::-1]
os.system("clear")
def menu():
print "-----------"
print "[1] Encrypt"
print "[2] Decrypt"
print "-----------"
if input(">>> ")==1:
encrypt()
elif input(">>> ")==2:
decrypt()

def encrypt():
os.system('clear')
text=raw_input(">>> ").lower()
text=list(text)
textnew=text
for i in range(len(text)):
textnew[i]=mixed[abc.index(text[i])]
print ''.join(textnew)
menu()

def decrypt():
os.system('clear')
text=raw_input(">>> ").lower()
text=list(text)
textnew=text
for i in range(len(text)):
textnew[i]=abc[mixed.index(text[i])]
print ''.join(textnew)
menu()

menu()

最佳答案

if input(">>> ")==1:
encrypt()
elif input(">>> ")==2:
decrypt()

您正在读取 elif 中的第二个输入。这就是为什么第一个命令似乎被忽略的原因。顺便说一下,Python 2 中的 input 是不安全的。您应该坚持使用 raw_input(它只返回一个字符串而不尝试对其求值)。

command = raw_input(">>> ")
if command=="1":
encrypt()
elif command=="2":
decrypt()

关于python - 几乎相同的功能,但不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52303349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com