我正在研究班级是如何工作的,并且我想出了如何在某些情景中让班级工作。但是我找不到让我的方案有效的方法。它涉及class Function():
,def __init__(self):
和def AnotherFunction(self, Foo):
。这些不是我场景中的实际“功能”,但它们非常相似。这里是我的代码:Python 3.5.1:我如何让while循环在类中运行?
class Name():
def __init__(self, Name):
self.Name = ["Your Dicer's name is: "]
self.AddName()
def AddName(self):
self.Count = 1
while(self.Input == 1):
Input = input("What is your Dicer's name?:\n")
if(any(BadWord in Input.lower() for BadWord in [])):
print("That's an inappropriate name! Try again")
elif(len(Input) < 3):
print("That name is too short! Try again.")
elif(len(Input) > 16):
print("That name is too long! Try again.")
else:
self.Count -= 1
self.Name.append(Input)
我的问题是,为什么不会发生在class
的while loop
?我试着查找其他堆栈溢出问题,类文章和教程,以及与while循环有关的其他问题,但无济于事。你能告诉我为什么它不会运行,以及如何解决这个问题?我将不胜感激。谢谢! :)
更新1:我把Input
在AddName parameters
。我还添加了Name
作为instance
。但是这些更新没有奏效。
更新2:我把Input
中的AddName parameters
中。我也将self.Input
更改为self.Count
,所以不会混淆。我也将self.Name += Input
更改为self.Name.append(Input)
。这些更新使代码更容易理解并修复了一些问题,但while loop
不会运行。
你可以不带必需的参数('self.AddName()')来调用你的函数,为参数'Input'提供一个参数或者改变函数定义为'def AddName(self):...'。 –
如果通过调用构造函数'x = Name()'来创建'Name'的实例,它会引发TypeError,因为'AddName'需要一个你不给它的参数,所以我假设问题是你是从不创建实例。 –
@Jim这两个建议都不起作用 – BradTheBrutalitist