无论给出什么输入,'raw_input'都不执行任何操作

时间:2014-06-28 20:16:21

标签: python python-2.7

我正在尝试创建一个工具,根据字典中的值对字符串进行编码/解码,但是,我坚持使用以下代码:

class edt():
    e_dic = {}

    def main(self):
        c = "hcqnxmytwukgirzoeaspdfvlbj"
        a = "abcdefghijklmnopqrstuvwxyz"
        e_dic = {}
        for i in range(len(c)):
            e_dic[a[i]] = c[i]
        e_dic[" "] = " "
        self.e_dic = e_dic
        e = edt()
        user_input = raw_input("1.Encode\n2.Decode\n")
        if user_input == "1":
            e.encode()
        elif user_input == "2":
            e.decode()
        else:
            False

    def encode(self):
        print("test")

    def decode(self):
        print("test")

def run():
    run_main = None
    run_main = edt()
    run_main.main()

我省略了encode()和decode(),因为我认为它们不会导致问题。 问题是当我运行它时会发生这种情况:

>>run()
1.Encode
2.Decode
>>
(The script pauses here to wait for the return key to be pressed, but does nothing regardless of the input given)
>>

完成后我没有错误。任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

代码看起来很好。但是你不应该使用一个对象来调用该方法吗?像这样

class edt():
e_dic = {}

def encode(self):
    print "encoded "

def decode(self):
    print "decoded "

def main(self):
    c = "hcqnxmytwukgirzoeaspdfvlbj"
    a = "abcdefghijklmnopqrstuvwxyz"
    e_dic = {}
    for i in range(len(c)):
        e_dic[a[i]] = c[i]
    e_dic[" "] = " "
    self.e_dic = e_dic
    e = edt()
    user_input = raw_input("1.Encode\n2.Decode\n")
    if user_input == "1":
        e.encode()
    elif user_input == "2":
        e.decode()
    else:
        False

def run():
    run_main = None
    run_main = edt()
    run_main.main()

输出:

>>> run()
1.Encode
2.Decode
1
encoded 

小提琴:

http://ideone.com/9eET4T

答案 1 :(得分:0)

您需要在编码和解码中使用self

class edt(object):
    def __init__(self):
        self.e_dic = {}
    def main(self):
        c = "hcqnxmytwukgirzoeaspdfvlbj"
        a = "abcdefghijklmnopqrstuvwxyz"
        for i in range(len(c)):
            self.e_dic[a[i]] = c[i]
        self.e_dic[" "] = " "
        user_input = raw_input("1.Encode\n2.Decode\n")
        if user_input == "1":
            self.encode()
        elif user_input == "2":
            self.decode()
        else:
            return False

    def encode(self):
        print("test")

    def decode(self):
        print("test")
def run():
    run_main = None
    run_main = edt()
    run_main.main()
run()