New to Python, can you please help with a question??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kylie991
    New Member
    • Feb 2009
    • 41

    New to Python, can you please help with a question??

    Hi I am new to Python. I am stuck on this question..... I know it is only simple but it just will not work for me and I have spent hours looking at the screen.. :(

    below is some code. The first fuction is to simply return the average rating of the video and a count.... I thought I did this right as when I create a separate shell that links to the database it provides the average and the count. (number of ratings added to the database) But when I run the the test script it comes up with an error message saying

    line 145, in test_average_ra ting
    (avg, count) = util.average_ra ting(vid)
    TypeError: 'NoneType' object is not iterable

    What does this mean???

    below is my code for that function and also another one.... I'm only very new to this so I am getting so frustrated with myself and am finding the university is not helping me much at all...
    Code:
    def average_rating(video_id):
        """Return the average rating for this video and
        the number of votes as a tuple (avg, count)"""
    
        con = connect()
        cur = con.cursor()
        
        average = cur.execute("select AVG(rating), count(rating) from ratings")
        for ratings in average:
            print ratings
    
    def add_rating(video_id, email, rating, comment):
        """Add a rating for a given video and user email.
        If the video or user doesn't exist, or if the rating is
        outside of the range 1-5, raise an exception of
        type WebappError with a message that can be
        shown to the user.
        """
    
        con = connect()
        cur = conn.cursor()
        
        cur.execute("INSERT INTO ratings(video, email, rating, comment) VALUES ('?','?','?','?')")
        conn.commit()
    Last edited by bvdet; Apr 1 '10, 10:44 AM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Add a print statement after you assign the cursor query to variable average. I think you will find that average is not an object you can iterate on.
    Code:
    >>> av = 1
    >>> for obj in av:
    ... 	print obj
    ... 	
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
    TypeError: iteration over non-sequence
    >>>
    What fields are in the ratings table?

    Comment

    Working...