The input routine for this program is very basic - it simply attempts to
read a number from standard input and store it in the number variable.
To start with, we will initialize number to an invalid value.
Initialize variables +=
number = 0
|
Notice that this has been appended to the "Initialize variables" code segment
that was started in the previous section. Now, we need to read in the
number.
Read in a line of input =
print """Please enter a positive integer, and I will add all numbers between one and
that number"""
textline = sys.stdin.readline()
try:
number = string.atoi(textline)
if number == 0:
raise InvalidNumberError
except:
print "You typed an invalid number. Exitting..."
sys.exit(1)
|
That code needs to have the string module and the sys module imported.
Therefore, we will add the imports to the beginning of the source file.
Import needed modules =
import sys, string
|
Finally, when the code is finished, we need to print the results.
Print results
print "When you add all the numbers from one to ", number, ", the result is ", total, "."
|