CS 115 Lab 3, Part A: Sum a list of numbers

[Back to lab instructions]


The Big Picture

The instructions below will help you write a program that asks the user for 5 numbers and computes their sum and mean.


Instructions

  1. Open IDLE. Go to the File menu and select "New File." 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()
    
  2. Go to "Save As", and save your file as lab03a.py in the default directory.
  3. Answer Questions 1-3 in your writeup. You can use the the online Python 3 tutor to answer these questions.
  4. If you modified your program to answer the questions in your writeup, change it back to the original version.
  5. 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
    
    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.
  6. 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
    
  7. 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.
  8. 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
    
  9. 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
    
  10. Call an instructor over to demonstrate your Part A code.
  11. Continue to Part B.