CS 0007: Fall 2009 Lab 4 YOUR NAME: YOUR PARTNER'S NAME: ****PART 1: Booleans and the Poisonous Potions Problem Remember: a and b and c and d is True only if ALL of a, b, c, d are True is False if ANY of a, b, c, d is False a or b or c or d is False only if ALL of a, b, c, d are False is True if ANY of a, b, c, d is True This part of your lab is about a logic puzzle involving four potions. You don't know which, if any, of the potions are poisonous, but you are given five hints: 1. At least one of the four potions is poisonous 2. p2 is not poisonous 3. At least one of p1 and p3 is poisonous 4. At least one of p3 and p4 is not poisonous 5. Exactly one of p1, p3, and p4 is poisonous Download puzzle.py from the course schedule. We use four boolean variables p1 to p4 to represent the potions: a True value means that the potion is poisonous, and a False indicates that it is not. The first hint has been translated into a Python function for you. hint1(False,False,False,False) will return False. This means that it isn't possible for all of the potions to not be poisonous, because then hint1 would be violated. However, hint1(False,True,False,True) will return True, meaning that p2 and p4 being poisonous, and p1 and p3 not being poisonous, will satisfy the first hint. However, the solution has to satisfy all five hints simultaneously. That's what function 'solution' tests - that all five hints are satisfied. If you give it a potential solution, such as False, True, True, False, it will return True if all five hints are satisfied, and False otherwise. Of course, it doesn't work yet -- you need to finish writing the other four hint functions. Once you are finished, call 'solution' with a solution. Trace the code in Wing, and make sure you understand why h1, h2, h3, h4, h5 have the values they do (the code is written with these separate values, so that you could see their values in the debugger). Also, make sure you know why "solution" returns what it does. Now, try to figure out a solution (there is more than one!) How do you do that? You reason like this, for example: p2 is not poisonous, no matter what (hint 2). p3 is in three of the hints. So, maybe reasoning about p3 is a good idea. p3 (like any boolean variable) is either True or False. Suppose p3 is True. Then, by Hint 4, we know that p4 is not poisonous. And so on ... Suppose p3 is False. Then, by hint 3, p1 is poisonous. And so on... Keep reasoning until you find a correct solution (even better - find two of them!). Do this until you are comfortable with what is going on with the code! ****PART 2: Practice reading code with while loops; using a list to make code clearer [NOTE: This part has been moved to lab 5] Download get_command.py from the course schedule. First, read, understand, and run get_valid_command1. The while loop in get_valid_command1 has condition True! So, it should run forever!! QUESTION: In what situation does the while loop terminate (without you killing the program). Explain how it terminates. ANSWER: Now, read, understand, and run get_valid_command2. There is no question to answer - just be sure you understand how it works. Now, let's see the power of lists!! TODO: uncomment and write function get_valid_command3 according to its docstring. Call it with different lists of valid commands, such as ["abs","max","min"], or ["media.get_red","media.get_green","media.show","media.set_color"]. See, you could use get_valid_command3 for ANY set of commands you care about. You could reuse this function any any number of programs where you are prompting the user for a command!