Homework 4
Due: Tuesday, Feb. 28 at 11:59pm on Moodle
Please take the short Week 4 quiz on Moodle (also due by Tuesday night). This is to help me understand what areas need work, and also help you understand what concepts might need extra practice. It is not a large part of the overall homework grade. Just for the quiz part, do not discuss the questions with other students, TAs, etc. (Note: quiz usually posted Wednesday afternoon.)
The following steps will help you with the specific details and program requirements. As you're developing your code, make sure to use comments and informative variables names.
First, begin from the Python file hw4A.py, which contains the lists of lab students so you don't have to type them all out. Right now these lists are alphabetized, but the goal is to shuffle the names around to generate random partnerships.
Inside this file, below the lists, write a function called swap(i, j, lst), which will take in two integer indices (i and j) and a list of elements lst, and swap the ith and jth elements. An example of how this method should work is shown below:
Note that the swap method will not RETURN or PRINT anything. It will MODIFY the parameter (lst). Make sure to test out your method in the shell to make sure it is working. You should be able to reproduce the examples above exactly.
The next method to write is shuffle(lst). This method will also not return or print anything, but instead modify the list. This method will generate random indices and call swap using these indides. Inside this method, perform at least 100 random swaps so that the list is well shuffled. Think about this part like shuffling a deck of cards, but the only operation you can do it to swap two randomly chosen chards. An example is shown below:
Since it is random, your shuffle method will likely produce different results than shown above.
Next, write a method called random_pairs(lst) that will call your shuffle function on the list, and then print out the pairs. This method will not return anything either, but it will print out the pairs. After shuffling the list, take the first two elements as the first pair, the next two as the second pair, etc (using a for-loop). An example is shown below (add a few spaces when printing so the pairs are indented slightly):
Note that some lists will have an odd number of names. In this case, print the last name on the same line as the last pair to form a group of three (think about how to do a special case for this part).
Finally, write a main() function that calls random_pairs on each of the four lab sections (not within a for-loop). Print out some custom header information for each section as shown in the example output below. Invoke main inside your file, so that each time you run your code, a new set of random partnerships is generated.
Note that here Wednesday and Thursday lab partners appear side by side so everything is on one screen, but in your output all of Thursday will follow all of Wednesday.
Rock beats ("smashes") scissors Scissors beats ("cuts") paper Paper beats ("covers") rock Same choices: tieAfter this "battle", the winner receives one point. Then this process is repeated for the desired number of rounds. At the end, the total score determines the overall winner. Your task is to program a rock-paper-scissors game where the user plays the computer.
Here is an example of what your program should do (I did not do very well against the computer):
This part of the assignment is more open-ended, but I've provided a few guidelines below. The main requirement is that you use at least one helper function, but I might suggest thinking about that after you've gotten the basic structure of your program working.
For Part A, generate lab partners for each week of the semester (we'll have roughly 10 labs total). Now add a condition: no one should have the same partner twice. Think about how to store the previous partner information for each person, and only sample from the remaining students.
For Part B, use a Boolean variable to keep track of whether or not the user still wants to play. If the user types "quit" at any point instead of "rock", "paper", or "scissors", the program should quit and print the final scores. Set the number of rounds very high to avoid running out of rounds before the user wants to quit (this is not very desirable, but later on we'll see another loop structure for this type of behavior).
Finally, create a transcript that demonstrates your code working (for Part A, run your program a few times to show different random partnerships, and for Part B show a few complete games with different numbers of rounds). Save this file as hw4_transcript.txt. Also make sure your code is commented, and that you have given some thought to the header, variable names, and whitespace.
TO SUBMIT ON MOODLE: