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

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 ..."
# an empty list to start with
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
"""

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 ..."
# an empty list to start with
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.

Just as a late clarification, lists are fairly different in Python from Arrays in C. I find lists much more pleasant to use, because:
- they are dynamically resizable, and no set length needs to be declared upon initialization, but you can't assign a value to an index past the current length of the string without appending it to update the list to the new length.
- they can contain any type of objects, i.e. mixed contents. One index may be a string, the next an int, then tuple, etc.

Anyways, you can read the documentation on lists and their built-in functions here if you'd like to know more.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.