如何使此字典对象在 while 语句下不断变化?



最初这个问题是用递归来解决的,但无论如何,我只是决定使用 while 语句。

如果需要递归,请告诉我。

这只是我代码的一部分,但我想问题是字典employee_state并且visitor_state不断更改,但更改不会应用于 while 语句。

我该如何解决这个问题?

另外,如果您需要更多我的代码来建议答案,请评论我。

def order_process(self):
    employee_state={}
    visitor_state={}
    for employee in self.employees:
        employee_state[employee]=employee.state
    for visitor in self.visitors:
        visitor_state[visitor]=visitor.state
    while WAITING in employee_state.values() and ARRIVAL in visitor_state.values():
        print(employee_state)
        print(visitor_state)
        for visitor in self.visitors:
            for employee in self.employees:
                if employee.state == WAITING:
                    if visitor.state == ARRIVAL:
                        employee.cook(visitor)
                        employee_state[employee] = employee.state
                        visitor_state[visitor] = visitor.state

一个快速简便的解决方法是将 while 语句更改为简单while True:,然后在 while 块的末尾添加以下条件

if WAITING not in employee_state.values() and ARRIVAL not in visitor_state.values():
    break

最新更新