The Python programming language is an ideal choice for rolling out
simple, straightforward scripts that perform basic computational tasks,
such as mathematics. In fact, the Python IDLE environment allows you to
simply enter mathematical expressions to accomplish quick calculations.
However, by extending your interactions with IDLE, you can quickly
design an easy program to calculate averages for student grades.
Instructions
- Python Interpreter (comes with IDLE)
-
-
1
Open IDLE. On a Windows
computer, click "Start," then "All Programs," then "Python" and then
"IDLE." On a UNIX computer, such as a Linux machine or a Mac, simply
open a command terminal and type "python" into the prompt.
-
2
Once in IDLE, declare a list
variable to hold grades. You will know you are in the IDLE environment
when the ">>>" prompt appears. Enter the following command to
declare a list variable that will hold grades:
>>>grades = list()
-
3
Set up an input loop, which will
fill the grades list. In this example, the letter 'q' signals that the
user is finished inputting grades. The user can enter as many grades as
required and can signal an end to entry by using an arbitrary entry of
the 'q' character:
>>>x = 0
>>>grades.append(raw_input('Grades: '))
Grades:55
>>while grades[x] != 'q':
. . . grades.append(raw_input('Grades: '))
. . . x += 1
-
4
Use another loop to add the grades. The following loop adds grades until hitting 'q':
>>>x = 0
>>>for item in grades:
. . . if item == 'q':
. . . pass
. . . else:
. . . x += int(item) //convert from string to integer
-
5
Find the average of the grades.
This involves adding all the grades and dividing that number by the
length of the list minus one, to account for the 'q' character. This
also involves importing the "division" package to perform proper decimal
division:
>>>from __future__ import division
>>> x / (len(grades) - 1)
//average