为什么将此值设置为None?

时间:2017-08-02 17:32:15

标签: python python-3.x

我正在研究一个实验项目(创建一个基本的谜机机制),并且在重新分配变量时遇到了问题。我有一个"检查"检查以确保用户输入的值是1到26之间的数字的函数,如果不是,它再次调用输入函数,询问新值。如果我输入1到26之间的数字(例如:4),一切正常,但是,如果我输入29或" qwerty",它开始变得混乱。它再次调用输入函数(如预期的那样),如果我然后输入4作为值,变量将被指定为"无"。

我该如何解决这个问题?

工作时的CLI输出(例如:输入4):

What is the message you would like to encrypt?as
For the next 3 questions, please input ONLY a number from 1 to 26
What is the first key value?4
4

失败时的CLI输出(例如:输入28然后输入4):

What is the message you would like to encrypt?asd
For the next 3 questions, please input ONLY a number from 1 to 26
What is the first key value?28
You must input a number between 1 and 26!
What is the first key value?4
None

代码:

class Input:

    def message():
        msg = input("What is the message you would like to encrypt?")
        msg = msg.upper()
        print("\n For the next 3 questions, please input ONLY a number from 1 to 26")

    def check(input):
        try:
            if int(input) < 1 or int(input) > 26:
                return False
            else:
                return True
        except:
            return False

    def getKey(keyNum):
        word = ""
        if keyNum == 1:
            word = "first"
        elif keyNum == 2:
            word = "second"
        else:
            word = "third"

        s = input("What is the {} key value?".format(word))
        chk = Input.check(s)
        if chk:
            return(s)
        else:
            print("You must input a number between 1 and 26!")
            Input.getKey(1)

inp = Input
inp.message()
s1 = inp.getKey(1)
print(s1)

3 个答案:

答案 0 :(得分:0)

问题来自您的getKey()功能:

def getKey(keyNum):
    ...
    chk = Input.check(s)
    if chk:
        return(s)
    else:
        print("You must input a number between 1 and 26!")
        Input.getKey(1)

如果第一次调用Input.check(s)返回True,则getKey()会返回输入的值。但如果第一次调用返回False,则getKey()不会返回任何内容。它会再次请求输入,但是它从不会将输入传回给需要它的代码。您需要做的是向return块添加适当的else语句,以便返回来自getKey()的递归调用的值。

答案 1 :(得分:0)

在您的代码中:

    if chk:
        return(s)
    else:
        print("You must input a number between 1 and 26!")
        Input.getKey(1)

chk是&#34; truthy&#34; - 也就是说,当if chk:成功时 - 您返回s

但是当else:运行时,你不会返回任何东西。相反,你递归到getKey(1)(这是错误的 - 你应该使用参数,如果它是2或3)。

现在,对getKey(1)的递归调用将返回一个值。但是你忽略了这个价值而又没有回来。

既然你不回到那个分支,你就会经历&#34;到函数的最后,Python的默认机制开始了:如果函数没有返回任何值,Python会自动提供None作为结果。

您在else:案件中所获得的内容。

答案 2 :(得分:0)

你已经创建了类名Input,所以它的类函数应该在它们中有第一个参数self也从内部函数调用相同的函数,我们将通过self.name函数调用它,这样它就不会为其他变量调用其他函数但存在。
一个小错误就是调用Input.getKey(1),它应该像self.getKey(keyNum)一样正确地提供当前密钥。

class Input:

    def message(self):
        msg = input("What is the message you would like to encrypt?")
        msg = msg.upper()
        print("\n For the next 3 questions, please input ONLY a number from 1 to 26")

    def check(self,input):
        try:
            if int(input) < 1 or int(input) > 26:
                return False
            else:
                return True
        except:
            return False

    def getKey(self,keyNum):
        word = ""
        if keyNum == 1:
            word = "first"
        elif keyNum == 2:
            word = "second"
        else:
            word = "third"

        s = input("What is the {} key value?".format(word))
        chk = self.check(s)
        if chk:
            return s
        else:
            print("You must input a number between 1 and 26!")
            return self.getKey(keyNum)

inp = Input()
inp.message()
s1 = inp.getKey(1)
print(s1)

在类变量here

中调用成员函数的正确方法

看到它正常工作here

相关问题