my assignment is to write a program that asks the user to enter the name of the artist, the name of the art and then how much it is worth. At the end the program should print the average of the values. After that, it wants the program to sort it in alphabetical order by the artists name. all the information should be next to each other. for example

artist name artwork value
Van gouh .... 3.5

This is what i have so far

def main():
numPaintings = 7
totalValue = 0
Paint = numPaintings
numPaintings = input("Enter how many paintings there are")
while numPaintings != 0:
numPaintings = (numPaintings - 1)
pName = raw_input('Enter the Painting Name: ')
pArtist = raw_input('The Artist: ')
pValue = input('And its value (million): ')
totalValue = totalValue + pValue
Average = (totalValue / Paint)
print "the average is", Average
main()

please help thank you

This will get you started in the right direction, I'm storing the 3 pieces of data for a painting as a tuple. If you want to get the name, you index for list[0], and so on for the other two.

#returns a list of tuples
def createList():
    paintingList = []
    numPaintings = input("Enter how many paintings there are")
    for i in xrange(numPaintings):
        pName = raw_input('Enter the Painting Name: ')
        pArtist = raw_input('The Artist: ')
        pValue = input('And its value (million): ')
        tuple = (pName,pArtist,pValue)
        paintingList.append(tuple)
    return paintingList

def findAvg(list):
    length = len(list);
    total = 0
    for item in list:
        total += item[2] #indexed for the 3rd item in the tuple
#    avg = total / length #this is integer division if data is entered without period
    avg = float(total) / length #this will force to normal division
    
    return avg
    
def main():
    
    list = createList()
    print "the average is", findAvg(list)

if __name__ == '__main__':
    main()

I have not done the sorting for you, as you can figure it out more easily now. Below is an example of a simple selection sort, study it and see with a few simple edits if you can make it work with your program.

def selection_sort(list2):
    for i in range(0, len (list2)):
        min = i
        for j in range(i + 1, len(list2)):
            if list2[j] < list2[min]:
                min = j
        list2[i], list2[min] = list2[min], list2[i]  # swap

Code is taken from http://www.daniweb.com/code/snippet216689.html, which has more examples of other sorts.

the suggestion is otherwise ok, but I would not redefine built in type list.

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.