## The Champ

Question:

Prompt user to enter ten two digit number. The program should compute the sum of all positive values and print the sum of values added.

Over here what I had done is:

Chosen list to store initial all the ten numbers.
First I have to initialize the list with random numbers and then change them through user prompt.

I believe that I am doing it in wrong way. I would like to know the best possible way to use list for the given problem and matter of fact even would like to get opinion how to use list or what structure to use in place of C arrays.

The code is:

a=[]
for i in range ( 10 ):
(tab)a.append ( [] )

count = sum = 0
print 'Enter the 10 digits: '

for i in range ( 10 ):
(tab)a = int ( raw_input ( ) )
(tab)if a >= 0 and sum < 999:
(2tab)sum = sum + a
(2tab)count += 1

print 'The sum is', sum
print 'Total number: ', count

## The Champ

Thanks. I really wanted to see how to enter numbers in the list without initialization it completely. The second solution in your thread solved the problem.

Thanks once again.

## vegaseat 1,720

In Python you don't have to initialize a list. You simply start with an empty list and append to it ...

``````print "You are asked to enter ten 2 digit integers ..."
mylist = []
for k in range(10):
print k + 1,') ',
n = int(raw_input("Enter a 2 digit integer number: "))
mylist.append(n)

print mylist

print "sum =", sum(mylist)

"""
my result -->
You are asked to enter ten 2 digit integers ...
1 )  Enter a 2 digit integer number: 11
2 )  Enter a 2 digit integer number: 22
3 )  Enter a 2 digit integer number: 33
4 )  Enter a 2 digit integer number: 44
5 )  Enter a 2 digit integer number: 55
6 )  Enter a 2 digit integer number: 66
7 )  Enter a 2 digit integer number: 77
8 )  Enter a 2 digit integer number: 88
9 )  Enter a 2 digit integer number: 99
10 )  Enter a 2 digit integer number: 10
[11, 22, 33, 44, 55, 66, 77, 88, 99, 10]
sum = 505
"""``````

## The Champ

In Python you don't have to initialize a list. You simply start with an empty list and append to it ...

``````print "You are asked to enter ten 2 digit integers ..."
mylist = []
for k in range(10):
print k + 1,') ',
n = int(raw_input("Enter a 2 digit integer number: "))
mylist.append(n)

print mylist

print "sum =", sum(mylist)

"""
my result -->
You are asked to enter ten 2 digit integers ...
1 )  Enter a 2 digit integer number: 11
2 )  Enter a 2 digit integer number: 22
3 )  Enter a 2 digit integer number: 33
4 )  Enter a 2 digit integer number: 44
5 )  Enter a 2 digit integer number: 55
6 )  Enter a 2 digit integer number: 66
7 )  Enter a 2 digit integer number: 77
8 )  Enter a 2 digit integer number: 88
9 )  Enter a 2 digit integer number: 99
10 )  Enter a 2 digit integer number: 10
[11, 22, 33, 44, 55, 66, 77, 88, 99, 10]
sum = 505
"""``````

I think I know where I went wrong.
I was thinking in terms of C. So I thought that number can be directly entered in list as we do in array in C after declaring it. There is slightly different procedure in Python and then we don't need to carry out the sum.

Thanks once again.