如何使用 python 在状态机中定义最终状态
how to define a final state in a state machine with python
我有这个状态机:
class Chat(StateMachine):
begin= State('begin', initial=True)
state1=State('State1')
state2=State('State2')
go_to_state1=begin.to(state1)
go_to_state2=state1.to(state2)
def on_enter_begin(self):
global name
name = input("Please enter your name:\n")
self.go_to_state1()
def on_enter_state1(self):
global name
print("first state ")
value = input("hi "+name)
if value=="quit":
print('see you soon!')
break
else :
self.go_to_state2()
chat=Chat()
chat.on_enter_begin()
但我明白了:break
^
语法错误:'break' 外循环
有没有办法定义最终状态?如果用户键入 "quit" 状态机 exit/break ??
是的,您必须使用 return
而不是 break,因为您不在循环中:
if value=="quit":
print('see you soon!')
return True
我有这个状态机:
class Chat(StateMachine):
begin= State('begin', initial=True)
state1=State('State1')
state2=State('State2')
go_to_state1=begin.to(state1)
go_to_state2=state1.to(state2)
def on_enter_begin(self):
global name
name = input("Please enter your name:\n")
self.go_to_state1()
def on_enter_state1(self):
global name
print("first state ")
value = input("hi "+name)
if value=="quit":
print('see you soon!')
break
else :
self.go_to_state2()
chat=Chat()
chat.on_enter_begin()
但我明白了:break ^ 语法错误:'break' 外循环
有没有办法定义最终状态?如果用户键入 "quit" 状态机 exit/break ??
是的,您必须使用 return
而不是 break,因为您不在循环中:
if value=="quit":
print('see you soon!')
return True