The lessons for M02 introduce basic logic statements and program flow, such as the if and while statements>.
Both statements can be very useful in improving the way a program works, especially in collecting and validating user input. Although the user is ultimately responsible for entering valid data, useful programs will help make sure that invalid data is not entered by mistake and that the data that is entered is not obviously wrong.
Take a program that asks for bowling scores and calculates an average. If the user accidentally presses [enter] without entering anything, the program should not crash. It should alert the user that nothing was entered and allow another chance. Likewise, it the user enters a bowling score of 333 (which is not possible), such an entry should be ignored and allow the user another chance to enter a valid score.
By using a combination of while and if statements, programs can easily be built to provide a better user experience and more useful program.
M02 Assn 1 Exercise #1 (Calculate an Average)
The objective of this program is to collect a series of game scores from the user and calculate their average. The user should be able to enter as many or as few games as they want. This means you must keep track of how many games they enter.
Use a while loop to prompt the user, allow them to exit from entering scores, count the games, and the total score. Check to see if the user enters a special character or word ("q", "x", "quit" - whatever you like; put it in the prompt); if so, exit the while loop and then calculate an average (if they entered any games!). Display on separate lines the number of games they played, the total score and the average.
Tip: always convert the user entry using .lower(), so you can compare it with your exit word. For example, if your exit word is "quit", the user should be able to enter "QUIT", "Quit", "quit", etc.
Testing: I will make the following entries, in a random order: 111,222,89,120 (as shown below)
M02 Assn 1 Exercise #2 (Better Average Calculator)
The objective of this program is to calculate an average from user-entered scores. However, the program should now check to make sure that the user entry is either "q" or a number. (If the entry is not "q" [or whatever you are using for an exit word], you only need to check to see if the entry is empty (""). Otherwise, we will assume the user entered a number.) If a number, it should be more than 0 and less than 301 (like a bowling score). Importantly, the user should have multiple chances to make each entry.
You should be able to use quite a bit of code from Exercise #1. Use a second while loop to make sure the user entry is valid. If it is empty, less than 0 or more than 300, the entry is not valid, and the code should stay inside the entry loop. Otherwise, it can drop out of the entry loop and you can aggregate the data like before.
The design of this program uses both nested while loops and nested if statements. It will seem like a lot of code, but it is really not.
Testing: I will make the following entries, in a random order: 111,222,0,89,333,[enter],120 (as shown below)
M02 Assn 2 Exercise #3 (Calculate Compound Interest)
Assume a local bank is offering a savings account with annual compound interest of 5% or more. In other words, at the end of each year, the bank will calculate how much interest has been earned on the balance, and add that to the account (once per year, after the year is over).
Create a program to prompt the user for how much they want to deposit, how much interest they want to earn, and how many years they will keep it in the bank. For the interest rate, instruct the user (in your prompt) to enter a whole number, and then convert it to a FLOAT and multiply by .01 to use as a percentage.
Inside a for loop, calculate the amount of interest earned each year at the user's rate, and add it to the bank balance. Display 1) total amount of interest earned and 2) the bank balance after the number of years specified by the user.
Notice that you will be prompting the user for a FLOAT (deposit), a FLOAT(rate), and an INT (years).
As always, display the user's entries so they can visually confirm they are correct. (You can do this at the end of the output if you want, when you display the interest earned and the final balance.)
Submit your .py file to complete the assignment. (15 points)
Testing: I will make the following entries in 2 runs (10000,5,5) and (10000,6.5,6) (as shown below)
M02 Assn 2 Exercise #4 (Identify Secondary Color)
The colors red, blue, and yellow are known as "primary" colors. When mixing two primary colors, they produce a "secondary" color:
- Red and blue make purple
- Red and yellow make orange
- Blue and yellow make green
Create a program that does the following:
- prompt the user to enter the name of a primary color
- if the user entry does not match "red", "blue" or "yellow", display an error message (the program should then end)
- if the first color is valid, prompt the user to enter a second color
- again, if the second color does not match "red", "blue" or "yellow", display an error message (the program should then end)
- If both colors are valid, determine what secondary color is created by mixing both colors
- display a complete message to the user, such as "Mixing color x and color y will create z"
Submit your .py file to complete the assignment. (15 points)
Test values: I will use the following values to test your program:
- Run 1: white, BLUE (should display an error message for color 1)
- Run 2: RED, green (should display an error message for color 2)
- Run 3: RED, blue (should indicate that they make purple)
- Run 4: blue, yellow (should indicate that they make green)
This program requires a nested IF-ELSE statement. In other words, IF color 1 is valid, the second user prompt and code to find a secondary color should reside inside the first IF statement. Below are an example of a nested IF statement, an example of using GLOBAL variables for the primary colors, and a suggested format for the IF condition that checks the user's entry.