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
Read in a line of input =
print """Please enter a positive integer, and I will add all numbers between one and
textline = sys.stdin.readline()
number = string.atoi(textline)
if number == 0:
print "You typed an invalid number. Exitting..."
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 "When you add all the numbers from one to ", number, ", the result is ", total, "."