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

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

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.

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.

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.

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

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.

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!

This question has already been answered. Start a new discussion instead.