RSS Forums RSS
Please support our Python advertiser: Programming Forums
Views: 5034 | Replies: 7
Reply
Join Date: Mar 2005
Location: Nebraska, U.S.
Posts: 191
Reputation: stupidenator is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

break statement

  #1  
Jun 12th, 2005
Hi everyone.
This is my first post in the python thread and I am very new to the language. A while ago, i wrote a program that find the least common denominator for an array of numbers in Java. I recently started to look at the python language and I have attempted to write the same program in python. I wrote it and it works up to just one part. I have a 'while' loop with an inner 'for' loop. inside the for loop it has an 'if' statement that has a result of a 'break'. From what I have read in the Python Pocket Reference by O'Reilly, and from what I know about Java, the break statement should just break out of the 'for' loop and not the outer 'while' loop, but it seems like it is breaking out of both of them... How can I get it to just get out of the 'for' loop without getting out of the outer 'while' loop? Thanks for any help in advance.

Oh, by the way... Sorry for not showing any code examples of my problem, but I have it saved on a usb flash card and I seem to have misplaced it at the moment.

-Nick
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2005
Location: Nebraska, U.S.
Posts: 191
Reputation: stupidenator is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: break statement

  #2  
Jun 13th, 2005
I found the card and here is an example of what I am talking about:

def getLCD(ls):
    Max = getMax(ls)
    lcd = Max
    length = len(ls)
    temp = 0

    while temp<length:

        for x in ls:
            if isDivisible(Max,x) is 1:
                temp=temp + 1
            else:
                temp=0
                Max+=lcd
                break # this break should only break out of the for loop

        return Max
Reply With Quote  
Join Date: Oct 2004
Posts: 2,548
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Solution Re: break statement

  #3  
Jun 13th, 2005
I think you have other problems. There are two (Java?) functions you used that are not part of Python.

getMax(ls)

If variable ls is a list then max(ls) will give you the maxium element of the list.

isDivisible(Max,x)

I need a more detailed explanation of its purpose. What does it do and what does it return? Most likely Python has something to match.

Break should break out of the loop it is called in, whether it's a for or a while loop.
May 'the Google' be with you!
Reply With Quote  
Join Date: Mar 2005
Location: Nebraska, U.S.
Posts: 191
Reputation: stupidenator is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: break statement

  #4  
Jun 14th, 2005
Sorry, I should have just included the whole program in the first post. I made the getMax() and the isDivisible() methods. I'll include the whole program here

#returns true if the 'a' is evenly divisible by 'b'
def isDivisible(a,b):
    if a%b is 0:
        return 1
    else:
        return 0
    
# returns the largest number in the list
def getMax(ls):
    aMax=0

    for x in ls:
        if x>aMax:
            aMax=x

    return aMax

# returns the Least Common Denominator for the numbers in the list
def getLCD(ls):
    Max = getMax(ls)
    lcd = Max
    length = len(ls)
    temp = 0

    while temp<length:

        for x in ls:
            if isDivisible(Max,x) is 1:
                temp=temp + 1
            else:
                temp=0
                Max+=lcd
                break # This should just break out of the 'for' loop, but it seems
                        # to break out of the while loop too.

        return Max


enter=input('Enter denominators separated by a comma: ')
print 'LCD is:'
print getLCD(enter)



>>> Sample Input: 2,4,6,8,10
<<< Sample Output: 20

The correct LCD for that input is 120, so I think that is why the break statement is doing something to it.


Thanks for replying by the way.
Reply With Quote  
Join Date: Oct 2004
Posts: 2,548
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Solution Re: break statement

  #5  
Jun 14th, 2005
After putting in a few temporary print statements to check the variables, I found the problem somewhat hidden by some extra empty lines. The return statement's indentation lined up with the for loop rather than the while loop! The culprit was not break but the wrongly positioned return.
[php]#returns true if the 'a' is evenly divisible by 'b'
def isDivisible(a,b):
if a%b is 0:
return 1
else:
return 0

# returns the largest number in the list
def getMax(ls):
aMax=0
for x in ls:
if x>aMax:
aMax=x
return aMax

# returns the Least Common Denominator for the numbers in the list
def getLCD(ls):
Max = getMax(ls)
lcd = Max
length = len(ls)
temp = 0

while temp<length:
for x in ls:
if isDivisible(Max,x) is 1:
temp=temp + 1
else:
temp=0
Max+=lcd
break
return Max # indentation problem was here, return lined up with for loop!


enter=input('Enter denominators separated by a comma: ')
#enter = (2, 4, 6, 8, 10) # for testing, LCD = 120
print 'LCD is:'
print getLCD(enter)
[/php]
May 'the Google' be with you!
Reply With Quote  
Join Date: Mar 2005
Location: Nebraska, U.S.
Posts: 191
Reputation: stupidenator is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: break statement

  #6  
Jun 14th, 2005
Thank you vegaseat! I never would have guessed that.
Reply With Quote  
Join Date: Oct 2004
Posts: 2,548
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Solution Re: break statement

  #7  
Jun 15th, 2005
Associating return with the wrong loop was my favorite mistake when I programmed in C. It only creates a problem at runtime and is hard to catch sometimes.

When I read that you were using an USB Flash Card, my first thought was that you also used Movable Python, the version of Python that can be installed on a Flash Card and moved from computer to computer. I used that when I wanted to run Python24 and Python23 on the same computer.

Anyway, the Movable Python standard distribution comes with SPE, a handy little editor/IDE that takes a while to get used too. It has, as a nice feature, a bracket along the gutter, grouping statements similar to my C# editor. That would have flushed out errors like that easily.

Editors like PythonWin have a whitespace option, that shows spaces as little gray dots, which helps with problems like that too.
May 'the Google' be with you!
Reply With Quote  
Join Date: Mar 2005
Location: Nebraska, U.S.
Posts: 191
Reputation: stupidenator is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: break statement

  #8  
Jun 15th, 2005
Thanks for the ideas. Right now, I've just been using the IDLE program that came with the python download, but I will definetely look into some other editors and the movable python. Haha that would allow me to program at work!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:44 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC