1

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()

3
  • To print the dictionary: from pprint import pprint then pprint(your_dict). You can read up about python's pretty printer if you want to know more about it. Commented Jul 11, 2011 at 3:04
  • When I run it, nothing prints though. Commented Jul 11, 2011 at 3:11
  • I didn't vote you down. I suggest you try to adapt the advice of @nren, @gnibbler and @Cryo given in there ansers: Insert some prints to see if the code actually does what you expect. Remove the exceptions or make them informative by rewriting them as @gnibbler told you below. If you want to be sure sure go step by step: Uncomment all your code. Add just rudimentary code line by line and run your program after each litle change and observe how the prints change (or change not) too until you got what you want. Commented Jul 11, 2011 at 12:48

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! When I run this script, nothing prints. Not even {}.
What you are doing now is debugging, or working out the bugs in your code. When debugging, it is important to be able to trace your program as it steps through the code to see why it is not printing something, or why it is not behaving as you expect it to behave. A simple technique you can use is to insert 'print' statements to print out the values of certain variables, like the ones that determine how many times your loops execute, to see why the print statement is never reached.
1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.