![]() |
Function won't exit on return!
:
# def find_items(url): |
"if ss_item is -1: break" only breaks out of the for loop. The while loop is still being executed, and therefore remains in an infinite loop.
I'd suggest, in these situations, to print out the relevant values, as they are assigned, to see what's happening. Furthermore, I'm not sure where you're defining "item", but you should define it in the local scope. |
I define item in the global scope because when i call the function again it will clear the list but i need to break out of the while loop also...... please explain what you intended!
|
If you want to break out of both loops at "if ss_item is -1: break", then use return and not break.
Your necessities for defining item in the global scope seems irritational. You have a better alternative somewhere that you could be using. However, I guess that's just a nitpick with where this program's situated at the time being. |
Correct however when i execute a return it won't exit the function it continues the function as if nothing happend! any help there!
That last return won't return the value! |
The last return won't return the value because it's never reached! When you break out of the for loop, the program remains in the while loop. Since you break out of the for loop with a "break" statement, it never even gets a return.
Try it yourself, throw in a print "ss_item" right before the line "if ss_item is -1: break". You'll see that it infinite loops as it breaks out of the for loop, then re-enters it. Every time never reaching a return statement. Please learn to debug your programs effectively, it's a useful ability. |
I have removed the while loop and i allways use a debugger but the issue still remains! it executes the return item twice and then continues on going with the loop!
|
The problem is that your program never reaches a return statement. The first return statement is here. I've highlighted important lines in red:
:
# for items in range(1,25):The second return statement is outside the while loop. Since this is an infinite loop, and since you never break out of it, the second return statement is never reached either. There is also a fair amount of code that is redundant. For example: :
# char_to_kill = url[ss_item-1:ss_item]Also, it's usual to use "==" instead of "is" when testing for equality. The "==" operator checks whether something is equal to something else. The "is" operator checks to see whether two variables refer to the same object. To use an analogy, the phrase "Tom and Bob are the same person", would be an example of "is", whereas "Tom is either a clone of Bob, or the same person", would be an example of "==". It's a little complex, but the rule of thumb is to use "==" by default. It may save you from a few unexpected results. |
Quote:
|
:
item = []Ok this is the code i have now the issue is that i am stuck on the last return! it executes twice and the continues with ss_item+=9 now to answer a few questions.... 1.This issue is only a problem if there is a next page ( no_next is set to 0) so the function has to be called again! 2. In the webpage there is two of the same objects therefore the first one checks for errors and the second one ends the program for example to find aa in the string "aa is aa" you will find it twice! |
| All times are GMT -5. The time now is 12:58 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC