我正在开发一个简化的帽子聊天机器人,并使用线程处理输入之间的时间安排。但是,当我传递l(列表)以创建线程时,l的类型现在为sorting_hat,因此它没有属性“ append”。如何在输入线程中保留一个列表对象?
旁注:这不是家庭作业项目。我是一名学生,正在为此做一个辅助项目,向高中生讲授CS外展课程,以增加对计算机科学的兴趣。所有帮助将不胜感激!
class sorting_hat:
def __init__(self, *args, **kwargs):
self.gryffindor = 0
self.ravenclaw = 0
self.hufflepuff = 0
self.slytherin = 0
self.start = 0
self.name = ""
self.sort()
def sort(self):
print("Oh, you may not think I’m pretty,\n" +
"But don’t judge on what you see,\n" +
"I’ll eat myself if you can find\n" +
"A smarter hat than me.\n" +
"There’s nothing hidden in your head\n" +
"The Sorting Hat can’t see,\n" +
"So try me on and I will tell you\n" +
"Where you ought to be.\n")
time.sleep(1.5)
self.prompt_init_response()
def prompt_init_response(self):
self.start = time.time()
l = []
t = threading.Thread(group=None, target=self.input_thread, args=(l,))
t.start()
flag10 = False
flag25 = False
while 1:
if l:
self.name = l[0]
break
elapsed = time.time() - self.start
if elapsed > 10 and flag10 is False:
flag10 = True
print("Well, well, well...")
print("Are you gonna just sit there or are you gonna introduce yourself? Name?")
elif elapsed > 25 and flag25 is False:
flag25 = True
print("I'll just doze off then until you give me your name... Zzzzz")
def input_thread(l, *args):
print(type(l))
name = input("Oh! A new student! What's your name?")
l.append(name)