Assignment 3 - ldkvd/CS101L GitHub Wiki

Welcome to the CS101L wiki!

You’ll need to use many types of loops for this assignment. Some may be nested in others, for instance, you may have an outer loop in a game that keeps playing, and an inner loop performing another iterative task. You’ll want to think and experiment with the best ways to implement. In this lab, the great prognosticator “Flarsheim”, will let the user choose a number in their head from 1 to 100. It will then ask the remainder of this number when divided by 3, 5, and 7. Your program must validate proper input on each. The remainder when divided by 3 can only be 0, 1, or 2. The remainders for 5 and 7 are different. To find what number the user is thinking of just find the number from 1 to 100 that has the same remainder for 3, 5, and 7 and astound them with the result. They are then asked if they want to play again. The player may enter Y or N only. You can work on this bottom-up or top-down. Bottom-up means working on individual low-level details first. For instance, possibly write the code to get input for the remainder of 3. Remember it has to validate that, so the loop should continue to run while the user gives bad input. Top-down means you work on the overall flow first without any details filled in. Possibly create the outer loop to continue playing, then with successive iterations keep applying more detail.

Answer: Image 1 Image 2 Image 3 Image 4

In my code, my whole program was nested under one big while loop, so that at the end, the computer can ask the user if they want to play the game again. Within the outer loop, there are other inner loops to perform a certain action.

The remainder when divided by 3 can only equal 0, 1, or 2, so I added a while loop that checks for the range. If the user input is out of the range (less than 0 or greater than 2), it will enter the loop. Within that loop, I used an if-else statement so that the computer can print out the correct error statement for the user.

The first for loop is used to iterate through each number from 0 - 100. The modulo operator is used to compute the remainder. If the remainder is equal to user input (for division by 3), then it is put into the three_list. The second loop iterates through each element in the three_list. The modulo operator is used to compute the remainder. If the remainder is equal to user input (for division by 5), then it is put into the five_list. The third loop iterates through each element in the five_list. The modulo operator is used to compute the remainder. If the remainder is equal to user input (for division by 7), then it is put into the seven_list.

The user's number is located in the seven_list. So, use a print statement to print the message and the user's number.

Still, within the outer while loop, a while loop and if statements are used to ask the user if they want to play again. If their input is true ('Y' or 'N'), it will run through the if statements. If the user inputs 'Y', it will break out of the inner while loop and enter the outer while loop to play again. If the user inputs 'N', the game will break and exit the game. If the user inputs anything other than 'Y' or 'N', it will ask the user if they want to play again until their input is true.