Help Me Debug, "TypeError: unpack non-sequence"

This is the place for queries that don't fit in any of the other categories.

Help Me Debug, "TypeError: unpack non-sequence"

Postby webmanoffesto » Wed Apr 24, 2013 8:39 am

Please help me debug the code below.

I checked this on http://www.pythontutor.com/visualize.html#mode=display and got the message
"TypeError: 'function' object is not iterable"

The error message I get when I run it on my computer is
"Traceback (most recent call last):
File "D:/From.Toms.Old.Desktop/Oranim/Teaching/Udacity.CS101/Lesson.02/Lesson.02..Section.30..Print.All.Links..v01.py", line 25, in -toplevel-
url, end_quote = print_all_links('this is a <a href="http://link1.com">link</a>, this is a <a href="http://link2.com">link</a>, this is a <a href="http://link3.com">link</a>')
File "D:/From.Toms.Old.Desktop/Oranim/Teaching/Udacity.CS101/Lesson.02/Lesson.02..Section.30..Print.All.Links..v01.py", line 8, in print_all_links
url, end_quote = get_next_target
TypeError: unpack non-sequence"

Code: Select all
print ""
print "CS101 unit2-30"
print "Print all the links on a page"
print "- Solution: https://www.udacity.com/course/viewer#!/c-cs101/l-48753036/e-48727558/m-48683657"

def print_all_links(page):
        while True:
            url, end_quote = get_next_target
            if url:
                print url
                page = page[endpos:]
            else:
                break
def get_next_target(page):
    start_link = page.find('<a href=')
    start_quote = page.find('"', start_link)
    http = page.find('http', start_link)
    www =  page.find('www', start_link)
    end_quote = page.find('"', start_quote + 1)
    url = page[start_quote + 1: end_quote]
    return url, end_quote


print "Test #1, Multiple Links"
url, end_quote = print_all_links('this is a <a href="http://link1.com">link</a>, this is a <a href="http://link2.com">link</a>, this is a <a href="http://link3.com">link</a>')
webmanoffesto
 
Posts: 6
Joined: Sat Apr 20, 2013 11:28 am

Re: Help Me Debug, "TypeError: unpack non-sequence"

Postby stranac » Wed Apr 24, 2013 8:52 am

Code: Select all
url, end_quote = get_next_target

You're trying to assign a function to two names.
What you want to do is assign the return value.
To get that, you need to actually call the function.

I believe you want:
Code: Select all
url, end_quote = get_next_target(page)


There are more problems with this(such as endpos being undefined), so this code will need some more work before it works.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1097
Joined: Thu Feb 07, 2013 3:42 pm

Re: Help Me Debug, "TypeError: unpack non-sequence"

Postby webmanoffesto » Wed Apr 24, 2013 9:14 am

Thank you so much for your fast answer. I fixed those two problems and it does print the three links now. But for some reason I still get an error message at the end. Please help me correct that problem.

http://ideone.com/Rx0asw#view_edit_box
Traceback (most recent call last):
File "prog.py", line 29, in <module>
url, end_quote = print_all_links('this is a <a href="http://link1.com">link</a>, this is a <a href="http://link2.com">link</a>, this is a <a href="http://link3.com">link</a>')
TypeError: 'NoneType' object is not iterable

Error:
"Traceback (most recent call last):
File "D:/From.Toms.Old.Desktop/Oranim/Teaching/Udacity.CS101/Lesson.02/Lesson.02..Section.30..Print.All.Links..v01.py", line 29, in -toplevel-
url, end_quote = print_all_links('this is a <a href="http://link1.com">link</a>, this is a <a href="http://link2.com">link</a>, this is a <a href="http://link3.com">link</a>')
TypeError: unpack non-sequence"

Code: Select all
print ""
print "CS101 unit2-30"
print "Print all the links on a page"
print "- Solution: https://www.udacity.com/course/viewer#!/c-cs101/l-48753036/e-48727558/m-48683657"

print "My forum posts regarding this"
print "http://python-forum.org/viewtopic.php?f=6&t=2699"
print "https://forums.udacity.com/questions/100051898/help-me-debug-typeerror-unpack-non-sequence#cs101"

def print_all_links(page):
        while True:
            url, end_quote = get_next_target(page)
            if url:
                print url
                page = page[end_quote:]
            else:
                break

def get_next_target(page):
    start_link = page.find('<a href=')
    start_quote = page.find('"', start_link)
    #http = page.find('http', start_link)
    #www =  page.find('www', start_link)
    end_quote = page.find('"', start_quote + 1)
    url = page[start_quote + 1: end_quote]
    return url, end_quote

print "Test #1, Multiple Links"
url, end_quote = print_all_links('this is a <a href="http://link1.com">link</a>, this is a <a href="http://link2.com">link</a>, this is a <a href="http://link3.com">link</a>')
Last edited by webmanoffesto on Wed Apr 24, 2013 9:24 am, edited 1 time in total.
webmanoffesto
 
Posts: 6
Joined: Sat Apr 20, 2013 11:28 am

Re: Help Me Debug, "TypeError: unpack non-sequence"

Postby stranac » Wed Apr 24, 2013 9:21 am

Code: Select all
url, end_quote = print_all_links(...)

There is no return inside print_all_links(), so it returns None.
You can't unpack None(assign it to 2 names).
You should just use:
Code: Select all
print_all_links(...)


Btw, which version of python are you using and how are you running this?
Your error messages are kinda non-descriptive.
Here's what I get:
Code: Select all
TypeError: 'NoneType' object is not iterable
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1097
Joined: Thu Feb 07, 2013 3:42 pm

Re: Help Me Debug, "TypeError: unpack non-sequence"

Postby webmanoffesto » Wed Apr 24, 2013 9:35 am

Wow, thank you for all your help. It runs beautifully now. :D

I'm running Python 2.7
The "TypeError: 'NoneType' object is not iterable" errror message came from the debugger I used at http://ideone.com/.

Traceback (most recent call last):
File "prog.py", line 29, in <module>
url, end_quote = print_all_links('this is a <a href="http://link1.com">link</a>, this is a <a href="http://link2.com">link</a>, this is a <a href="http://link3.com">link</a>')
webmanoffesto
 
Posts: 6
Joined: Sat Apr 20, 2013 11:28 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests

cron