I get an error with this code and I dont know why I thought that sum was part of the array calss and it does come up when I put the "." after the array. This is the error I get.

Traceback (most recent call last):
File "F:/Python_Stuff/CSC143-901/score-array.py", line 22, in <module>
print 'The Home team score is ', homescore.sum
AttributeError: 'array.array' object has no attribute 'sum'

Below is my code. The loop is just 2 now for debug

#use arrays to track baseball scores
from array import *
#yearArray = array('i', [1776, 1789, 1917,1979])
print """you are entering score for 9 innings of a ball game for the home \n
         and visiting teams in the program. """
#Make arrays for teams score
homescore = array('i',[])
visitingscore = array('i',[])

# Loop for the 9 inning ball game
for x in range(0,2,1):
    home = int(raw_input('The home team score this inning. '))
    homescore.append(home)


# Loop for the 9 inning ball game
for x in range(0,2,1):
    visiter = int(raw_input('The visiting team score this inning. '))
    visitingscore.append(visiter)


print 'The Home team score is ', homescore.sum

print 'The Visiters team score is ', visitingscore.sum

#Decide who wins
if homescore.sum > visitingscore.sum:
    print"The Home team wins!"

else:
    print"The visiting team wins!"

Recommended Answers

All 4 Replies

homescore = array('i',[])

visitingscore = array('i',[])

Python uses list keyword or this sign [] as array.
That is pythonic system.

The array module is kind of one of those things that you probably don't have a need for if you don't know why you would use it (and take note that I'm not trying to say that in a condescending manner!). Most of the time, the array module is used to interface with C code.

Use list as i have canged in your code.
sum(list/tuple) not list/tuple.sum.

#The normal way off making list/array in python
homescore = []
visitingscore = []

for x in range(2):
    home = int(raw_input('The home team score this inning. '))
    homescore.append(home)

for x in range(2):
    visiter = int(raw_input('The visiting team score this inning. '))
    visitingscore.append(visiter)

print 'The Home team score is ', sum(homescore)
print 'The Visiters team score is %d ' % sum(visitingscore) #with string formatting

if sum(homescore) > sum(visitingscore):
    print"The Home team wins!"
else:
    print"The visiting team wins!"

OK Thanks, that helps out alot. I also appreciate the time you took to put the data formatting in. The 3D gallery was really cool.

Sum does work, but it is not method of array:

>>> from array import *
>>> myarray=array('i',range(10))
>>> myarray
array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> sum(myarray)
45
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.