2017-08-02 76 views
0

我正在研究一个实验项目(创建一个基本的谜机器机制),并且在重新分配变量时遇到了一个问题。我有一个“检查”函数,用于检查用户输入的值是1到26之间的数值,如果不是,它会再次调用输入函数,要求一个新值。如果我输入一个介于1和26之间的数字(例如:4),一切正常,但是,如果我输入29或“qwerty”,它会开始混乱。它再次调用输入函数(如预期的那样),如果我然后输入4作为值,则变量被赋值为“无”。为什么这个值被设置为None?

我该如何解决这个问题?它

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) 
+0

后您解决问这里的问题,你可能要头以上[ codereview.SE]来获得关于构造代码的一些常规技巧。 –

回答

0

你已经创建的类名Input所以它的类的函数应该在他们的第一个参数自也呼吁从内部功能相同的功能,我们将通过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

+0

我试图按照你所建议的方式使用self,将它添加到参数中,但是现在我得到错误“TypeError:message()missing 1 required当我尝试调用该函数时,位置参数:'self'“。 – Trilliante

+0

@Trilliante试试看,我编辑了答案。顺便说一下,这是使用类时的正确实现。查看答案的最后一个链接来检查。 –

+0

啊,它似乎没有工作,因为我有“INP =输入”而不是“INP =输入()” – Trilliante

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()的值。

0

在你的代码在这里:

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

chk是 “truthy” - 也就是说,当if chk:成功 - 你做的s回报。

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

现在,递归调用getKey(1)将返回一个值。但是你忽略了这个价值,不要回报。由于你没有返回到那个分支,所以你“落在”了函数的末尾,在这里Python的默认机制开始了:如果一个函数没有返回任何值,Python自动提供一个None作为结果。

这就是你在else:的情况下得到的结果。

+0

阅读https://stackoverflow.com/a/25825737/4465743 –