In this prelab you will formulate some of the ideas necessary to complete Lab 04. Please hand in a paper copy of your solutions in class on Wednesday. Remember, no late prelabs allowed!
def main() : blarg(5) blorf(3) def blarg(x) : print(x*x) def blorf(y) : blarg(y+1) print(y) blarg(y-1) main()
def main() : x, y = 2, 7 print(x, y) x = blunk(x, y) print(x, y) def blunk(y, x) : print(x, y) x = 5 return x*y main()
3. Write a simple program with the following behavior. First, it should prompt the user to enter four numbers (x1, y1, x2, y2) separated by commas.
(Note: x1,y1,x2,y2 = eval(input("please enter 4 numbers separated by commas: ")) will achieve this.)
It should then compute and print the slope of the line that joins the points (x1, y1)and (x2,y2). In other words, it should set variable m to (y2-y1)/(x2-x1) and then print the value of m. There are a lot of things that could go wrong with this -- the user might omit the commas (giving a SyntaxError) or the user might enter only 3 numbers (giving a ValueError) or the user might enter numbers with the same x-coordinates, yielding a ZeroDivisionError.
Your code should handle all three of these exceptions.
Mastermind is a neat (although oftentimes frustrating) puzzle game. It works a something like this: There are two players. One player is the codemaker (your porgram), the other is the codebreaker (the user). The codemaker chooses a sequence of four colored pegs, out of a possible six colors (red, blue, green, yellow, orange, and purple). He may repeat colors and place them in any order he wishes. This sequence is hidden from the codebreaker. The codebreaker has 10 chances to guess the sequence. The codebreaker places colored pegs down to indicate each of her guesses. After each guess, the codemaker is required to reveal certain information about how close the guess was to the actual hidden sequence.
Describe the Problem:
The problem you will solve on your lab is as follows.
input: repeatedly get guesses from the user, until they either guess the code, or run out of guesses.
goal: generate a random code, and correctly provide the user with feedback on their guesses.
Understand the Problem:
The trickiest part of this game is determining how to provide feedback on the codebreaker's guesses. In particular, next to each guess that the codebreaker makes, the codemaker places up to four clue pegs. Each clue peg is either black or white. Each black peg indicates a correct color in a correct spot. Each white peg indicates a correct color in an incorrect spot. No indication is given as to which clue corresponds to which guess.
For example, suppose that the code is RYGY (red yellow green yellow). Then the guess GRGY (green red green yellow) would cause the codemaker to put down 2 black pegs (since guesses 3 and 4 were correct) and 1 white peg (since the red guess was correct, but out of place). Note that no peg was given for guess 1 even though there was a green in the code; this is because that green had already been "counted" (a black peg had been given for that one).
As another example, again using RYGY as our code, the guess YBBB would generate 1 white peg and 0 black; yellow appears twice in the code, but the guess only contains one yellow peg. Likewise, for the guess BRRR, only 1 white peg is given; there is an R in the code, but only one.
Check here for an online graphical version of the game.
4. Assuming the code is RYGY, fill in the appropriate number of black and white pegs for each guess.
|guess||black pegs||white pegs|
set a white counter to 0 loop through the four positions of the guess loop through the four positions of the code if the current code character matches the current guess character and the guess and code positions are not the same, increment the white counter and exit the inner loopWhen the actual code is RYGY, what does this algorithm give as a response to the guess YYYG? This is the wrong response; the correct one is 1 black, 2 white. Explain what the problem is
If you followed the Honor Code in this assignment, write the following sentence attesting to the fact at the top of your homework.
I affirm that I have adhered to the Honor Code in this assignment.