我一直在编写一个代码,我必须编写一个程序来模拟当老鼠进入厨房时,当陷阱被设置和未设置时会发生什么,它应该读入多行输入,描述厨房里发生的事情.
如果线路设置陷阱。进入,然后鼠标陷阱成为诱饵。如果线鼠标!输入,程序应该打印鼠标发生的事情。如果设置了陷阱,则打印出 Trap!。如果未设置陷阱,则打印出 The mouse escaped.. 对于所有其他输入行,程序不应执行任何操作。因为它应该一直读取用户的行,直到用户输入一个空行。一旦老鼠被困在陷阱里,除非它被重置,否则陷阱不能用来捕捉另一只老鼠。
所以我制作了这个程序,但它不适用于“鼠标逃脱”。任何人都可以通过查看我的代码来指导我:
a = raw_input("Enter line: ")
space = ""
trap_set = True
while a != space:
a = raw_input("Enter line: ")
if 'Set the trap.' in a:
trap_set = True
print "Trap!"
else:
if 'Mouse!' in a:
trap_set = True
print "The mouse escaped."
我希望我的程序像这样运行:
Enter line: Go to the fridge.
Enter line: Set the trap.
Enter line: Go to bed.
Enter line: Mouse!
Trap!
Enter line:
或者
Enter line: Make some dinner.
Enter line: Wash the dishes.
Enter line: Mouse!
The mouse escaped.
Enter line: Sweep the floor.
Enter line: Set the trap.
Enter line: Mouse!
Trap!
Enter line: Go to bed.
Enter line:
这就是我无法解决的问题。