Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 几乎相同的功能,但不起作用?_Python_Python 2.x - Fatal编程技术网

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

Python 几乎相同的功能,但不起作用?,python,python-2.x,Python,Python 2.x,我使用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',

我使用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()

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


以下是一些建议:

  • 使用原始输入
  • 不要重复你自己-不需要打印4条语句
  • 您的函数打印相同的提示。这会让用户感到困惑。至少添加一些关于发生了什么的指示
  • 不要递归调用
    main()
    (并最终限制堆栈上可以调用多少函数),而是让
    main()
    有一个每次调用函数的循环
  • 养成编写
    的习惯,如果_name__='\u_main__':
    ,因为当您开始编写Python模块时,您不希望脚本在导入时运行所有内容,而只是向调用脚本提供函数
  • 以80个字符换行
编辑代码:

导入操作系统
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’、‘s’、‘t’、‘u’、‘v’、‘w’、‘x’、‘y’、‘z’、’,
',', '1', '2', '3', '4',   '5', '6', '7', '8', '9', '0', '+', '-', 
':', "'"]
混合=abc[:-1]
操作系统(“清除”)
def菜单():
标头='\n'。加入([“-----------”,“[1]加密”,
[2]解密“,”------------“])
尽管如此:
打印页眉
用户输入=原始输入(“>>”)
#打印“调试:用户输入:”,用户输入
如果用户输入='1':
加密()
elif user_input==“2”:
解密()
elif user_input=='q':
退出()
其他:
打印(“错误输入”)
def get_input():
操作系统(“清除”)
打印“加密”
text=原始输入(“>>>”).lower()
返回列表(文本)
def encrypt():
text=get_text()
text新建=文本
对于范围内的i(len(text)):
textnew[i]=混合[abc.index(text[i])]
打印“”。加入(文本新建)
def decrypt():
text=get_input()
text新建=文本
对于范围内的i(len(text)):
textnew[i]=abc[mixed.index(text[i])]
打印“”。加入(文本新建)
如果uuuu name uuuuuu='uuuuuuu main uuuuuuuuuuuuu':
菜单()

您正在读取
elif输入(“>>>”)中的第二个输入==2
。这就是第一个命令似乎被忽略的原因。顺便说一句,Python 2中的
input
是不安全的。您应该坚持使用
raw\u input
,并显式地将返回的字符串转换为int。只需尝试使用单个输入,并根据该条件执行其中一个函数。事实上,现在我想到了两个函数重复代码,所以您可以将重复的片段提取到自己的函数中。
if input(">>> ")==1:
    encrypt()
elif input(">>> ")==2:
    decrypt()
command = raw_input(">>> ")
if command=="1":
    encrypt()
elif command=="2":
    decrypt()