I have been working on a code in which I have to write a program to simulate what happens when mice enter the kitchen when the traps are set and not set, and it should read in multiple lines of input, which describe what happens in the kitchen.
If the line Set the trap. is entered, then the mouse trap becomes baited. If the line Mouse! is entered, the program should print what happens to the mouse. If the trap was set, print out Trap!. If the trap wasn’t set, print out The mouse escaped.. For all other lines of input, the program should not do anything. as it should keep reading lines from the user until the user enters a blank line. Once a mouse is caught in the trap, the trap cannot be used to catch another mouse unless it has been reset.
So I have made the program but it’s not working for “The mouse escaped”. Can anyone please guide me by seeing my code:
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."
I want my program to run like this:
Enter line: Go to the fridge.
Enter line: Set the trap.
Enter line: Go to bed.
Enter line: Mouse!
Trap!
Enter line:
OR
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:
and this what I am not able to work out.
There were a couple of things mixed up here. If you call ‘Mouse!’ on the first call, it will fail because you have to set in the conditionals first.
The last two conditionals are where you are checking to see if the ‘trap’ was set when your ‘mouse’ was entered. Then reset the trap.