Homework 2 (due 4/16)
CSC 241



We talked more about logic, as well as strings and lists (Sections 2.2 and 2.3) and have covered some basic control flow, namely decision (one-way, two-way, multi-way), see Section 3.2. Next week, we will talk about iterations and start defining our own functions (3.2, 3.3).


Submission: The homework is due by midnight (I will not accept late homeworks). You can submit your homework through d2l into the drop-box for this homework. Please prepare your homework as a single file containing all answers (e.g. doc, docx, or pdf, not a zip file).

When submitting programs, include the programs, together with screenshots of test-runs of your program (make sure screenshots are sized so the text is legible; resize and/or crop the images).


1. (Reading Assignment) Read Sections 2.2, 2.3, 3.1, and 3.2 (through page 62). If you want to read ahead, finish Section 3.2  amd ,pve pm tp 3.3-3.5.

2. (Decisions, 12pt). Pig-latin is a popular way to disguise a word. It's simple: you take the first letter of the word, move it to the end, and then add 'ay'. For example, 'latin' turns into 'atinlay'. 'pig' becomes 'igpay'.

a) [5pt] Write a program that implements the basic pig-latin rule as described above. Test with some words.

b) [7pt] The simple rule above is incomplete. If the word starts with a vowel, a different rule applies: you simply add 'yay' at the end of the word, without moving the first letter to the end. So 'example' becomes 'exampleyay', or 'add' turns into 'addyay'. Write a program that implements both rules correctly, and decides which one to apply based on the first letter of the word. Note: submit this separately from part a.

Hint: you want one program, so you need to modify control flow based on whether the first letter is a vowel. How do you know? Use logic, or use a list of vowels to see whether the first letter belongs to the list. Note: vowels are a, e, i, o and u.

3. (More Decision, 13pt) If there is a vote at a meeting, there are several possible outcomes based on the number of yes and no votes (abstains are not counted). If all the votes are yes, then the proposal passes "unanimously", if at least 2/3 of the votes are yes, then the proposal passes with "two-thirds majority", if (strictly) more than 1/2 of the votes are yes, then the proposal passes by "simple majority", and otherwise it fails. Write a program that prompts the user for the number of yes and no votes (two separate prompts), and then prints the outcome of the vote. Hint: In a first step compute the total number of votes. Then remember if, elif, else. For this problem be careful about the boundary cases: a 6 out of 9 is a two-thirds-majority, but 5 out of 10 is not a simple majority (two-thirds requires "at least", simple majority requires "more than).

4. (Arithmetical guessing game, 15pt) Start with the arithmetial guessing game we implemented in class (you can find the code on d2l). Modify the code to add the following three features (you can add each feature to the original code separately and hand in each answer separarely (the recommended, safe way), or all at once, your choice).

a) [5pt] Allow the modulus operator '%' as one of the operations, e.g. the puzzle could be '7 % 4', answer would be 3. Note: Modulus questions should be picked (on average) as often as any of the other questions. Include a test-run specifically for the modulus. You may have to restart the game a couple of times to get that.

b) [5pt] Ensure that for subtraction problems, the first number is always at least as large as the second number (so there is no negative result). Hint: if the first number is smaller than the second number, simply swap them.

c) [5pt] Give the user some feedback on the quality of the answer. If the answer is correct, good, tell them it is. If their answer is not correct, but within +/-2 of the correct answer, tell them: "that's close, but not correct". E.g. for 13, 14, 16 and 17 would be close answers for 3*5. If the answer is off by more than 2 either way, tell them: "that is not close to being correct". Note: the abs() function may come in handy.


Marcus Schaefer
Last updated: April 10th, 2019.