Using Loops to track user input

Gerhard Häring gh at ghaering.de
Mon Jul 7 00:22:44 CEST 2003


Hi hokiegal99 if that's what you want me to call you,

hokiegal99 wrote:
> I don't understand how to use a loop to keep track of user input. Could 
> someone show me how to do what the program below does with a loop?

Your text book or tutorial should show that quite well, but here we go:

> 
> Thnaks!
> 
> ----------------------------
> #Write a program that reads 10 numbers from the user and prints out the 
> sum of those numbers.
> 
> num0 = input("Enter a number: ")
> num1 = input("Enter a number: ")
> num2 = input("Enter a number: ")
> num3 = input("Enter a number: ")
> num4 = input("Enter a number: ")
> num5 = input("Enter a number: ")
> num6 = input("Enter a number: ")
> num7 = input("Enter a number: ")
> num8 = input("Enter a number: ")
> num9 = input("Enter a number: ")
> 
> num = num0+num1+num2+num3+num4+num5+num6+num7+num8+num9
> 
> print num
> ----------------------------------

#v+
sum = 0.0
for i in range(10):
     sum += int(raw_input("Enter a number: "))
print sum
#v-

If your prof is any good, (s)he reads this newsgroup as well ;-)

-- Gerhard





More information about the Python-list mailing list