The instructions below will help you write a program that asks the user for
5 numbers and computes their sum and mean.
- Open PyCharm and then the project Lab03. Create a new python file an call it lab03a.py. Copy-paste the following program into the new window, substituting your name for the italicized text:
"""
Program: CS 115 Lab 3a
Author: Your name
Description: This program adds up the numbers from 1 to 5.
"""
def main():
total = 0
for i in range(1, 6):
total = total + i
print('The total is:', total)
main()
- Answer Questions 10-12 in your writeup. You can use the the online Python 3 tutor to answer these questions.
- If you modified your program to answer the questions in your writeup, change it back to the original version.
- Over the next few steps, you will modify your program to ask the user for 5 integers and then print
their sum. Here is an example of what your program will eventually do:
Enter an integer: 2
Enter an integer: 100
Enter an integer: -1
Enter an integer: 80
Enter an integer: 4000
The total is: 4181
The mean is: 836.2
If you feel ready to write this program, try it! If you get it working, skip to Step 9.
Otherwise, work through the following steps.
- Modify your original program to prompt for an integer and read the user's input into a new variable. Put this
new statement inside the loop so that it happens 5 times.
After you do this step only, you will be asking the user for 5 integers, but your program
will still print the sum as 15:
Enter an integer: 2
Enter an integer: 100
Enter an integer: -1
Enter an integer: 80
Enter an integer: 4000
The total is: 15
- Now, change the code so that every time you go through the loop, instead of adding i to the running total,
you add the number that the user just typed.
- Verify that your program's output matches this example:
Enter an integer: 2
Enter an integer: 100
Enter an integer: -1
Enter an integer: 80
Enter an integer: 4000
The total is: 4181
- Compute the mean by dividing the total by 5. You should only need to do
one division operation. Verify that your program's output matches this example:
Enter an integer: 2
Enter an integer: 100
Enter an integer: -1
Enter an integer: 80
Enter an integer: 4000
The total is: 4181
The mean is: 836.2
- Call an instructor over to demonstrate your Part A code.
- Continue to Part B.