Chris Bennett
Chris Bennett

Reputation: 51

More than one for-loop in a python while-loop

I'm a python/coding newbie and I'm trying to put a two for loops into a while loop? Can I do this? How can I print out the dictionary mydict to make sure I am doing this correctly?

I'm stuck.


40 minutes later. Not stuck anymore. Thanks everyone!

def runloop():
    while uid<uidend:
        for row in soup.findAll('h1'):
            try:
                name = row.findAll(text = True)
                name = ''.join(name)
                name = name.encode('ascii','ignore')
                name = name.strip()
                mydict['Name'] = name
           except Exception:  
                continue

        for row in soup.findAll('div', {'class':'profile-row clearfix'}):
            try:
                field = row.find('div', {'class':'profile-row-header'}).findAll$
                field = ''.join(field)
                field = field.encode('ascii','ignore')
                field = field.strip()
            except Exception:
                continue
            try:
                value = row.find('div', {'class':'profile-information'}).findAl$
                value = ''.join(value)
                value = value.encode('ascii','ignore')
                value = value.strip()
                return mydict
                mydict[field] = value
                print mydict
            except Exception:
                continue
    uid = uid + 1

runloop()

Upvotes: 1

Views: 471

Answers (3)

John La Rooy
John La Rooy

Reputation: 304167

You are not helping yourself by having these all over the place

       except Exception:  
           continue

That basically says, "if anything goes wrong, carry one and don't tell me about it."

Something like this lets you at least see the exception

       except Exception as e:
           print e  
           continue

Is mydict declared somewhere? That could be your problem

Upvotes: 1

nren
nren

Reputation: 2769

On nested loops:

You can nest for and while loops very deeply before python will give you an error, but it's usually bad form to go more than 4 deep. Make another function if you find yourself needing to do a lot of nesting. Your use is fine though.

Some problems with the code:

  • It will never reach the print statements because under the first for loop you have a return statement. When python sees a return inside a function, it will leave the function and present the return value.
  • I would avoid using try and except until you understand why you're getting the errors that you get without those.
  • Make sure the indentation is consistent. Maybe it's a copy and paste error, but it looks like the indentation of some lines is a character more than others. Make sure every tab is 4 spaces. Python, unlike most languages, will freak out if the indentation is off.
  • Not sure if you just didn't post the function call, but you would need to call runloop() to actually use the function.

Upvotes: 0

Cryo
Cryo

Reputation: 837

You can put as many loops within other loops as you'd like. These are called nested loops.

Also, printing a dictionary is simple:

mydict = {}
print mydict

Upvotes: 1

Related Questions