【问题标题】:Why is this value being set to None?为什么这个值被设置为无?
【发布时间】:2018-01-10 01:40:18
【问题描述】:

我正在进行一个实验项目(创建一个基本的谜机机制),并且在重新分配变量时遇到了问题。我有一个“检查”功能,用于检查以确保用户输入的值是 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)

【问题讨论】:

  • 解决您在此处询问的问题后,您可能需要前往Code Review 获取有关构建代码的一般提示。

标签: python python-3.x


【解决方案1】:

你已经创建了类名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

【讨论】:

  • 我尝试按照您建议的方式使用 self,方法是将其添加到参数中,但现在我收到错误“TypeError: message() missing 1 required positional argument: 'self'”我尝试调用该函数。
  • @Trilliante 试试看,我已经编辑了答案。顺便说一句,这是使用类时的正确实现。请参阅要检查的答案的最后一个链接。
  • 啊,它似乎不起作用,因为我有“inp = Input”而不是“inp = Input()”
  • @Trilliante 请务必更改已接受的答案,因为它具有误导性。
【解决方案2】:

问题出在你的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() 不会返回任何内容。它再次要求输入,您会看到,但它永远不会将该输入传达回需要它的代码。您需要做的是在else 块中添加适当的return 语句,以便返回来自对getKey() 的递归调用的值。

【讨论】:

    【解决方案3】:

    在您的代码中:

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

    chk 是“真实的”时——也就是说,当if chk: 成功时——你会返回s

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

    现在,对getKey(1) 的递归调用将返回一个值。但是您忽略了该值并且不返回。

    由于您没有在该分支中返回,因此您“跌倒”到了函数的末尾,Python 的默认机制在此启动:如果函数没有返回任何值,Python 会自动提供 None 作为结果。

    这就是您在 else: 案例中得到的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2016-04-02
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多