Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Problem with ifs and fors

Name: Anonymous 2011-08-14 7:18

I've only recently started trying to learn Python; I'm using Project Euler for some practice, and I'm currently doing Problem 24

I'm sure my code is not the most efficient out there, and it looks quite silly, but it gives me the correct answer and doesn't take too long, so I'm happy with it for now.

I have it printing out the correct answer, but then the program is still running, working out the next numbers in the sequence. I'd appreciate any help in getting my program to stop running once it's output the correct answer. Here is my code:

for b in range(0,10):
    if b != a:
        for c in range(0,10):
            if c not in (a,b):
                for d in range(0,10):
                    if d not in (a,b,c):
                        for e in range (0,10):
                            if e not in (a,b,c,d):
                                for f in range(0,10):
                                    if f not in (a,b,c,d,e):
                                        for g in range(0,10):
                                            if g not in (a,b,c,d,e,f):
                                                for h in range(0,10):
                                                    if h not in (a,b,c,d,e,f,g):
                                                        for i in range(0,10):
                                                            if i not in (a,b,c,d,e,f,g,h):
                                                                for j in range(0,10):
                                                                    if j not in (a,b,c,d,e,f,g,h,i):
                                                                        z = 1000000000*a + 100000000*b + 10000000*c + 1000000*d + 100000*e + 10000*f + 1000*g + 100*h + 10*i + j
                                                                        m = m + 1
                                                                        if m == 1000000:
                                                                            print z

Name: Anonymous 2011-08-14 19:50

it seems to me like you'd need to break at the bottom of every for loop after you've found the answer. So create a variable called done, set equal to False, and check for that everytime at end of for loop. if done (== True) then break. Do it for every for loop at end of for loop. So.... I'd do something like:


done = False
m = 9876543*2 for a in range (1,10):

for b in range(0,10):
    if b != a:
        for c in range(0,10):
            if c not in (a,b):
                for d in range(0,10):
                    if d not in (a,b,c):
                        for e in range (0,10):
                            if e not in (a,b,c,d):
                                for f in range(0,10):
                                    if f not in (a,b,c,d,e):
                                        for g in range(0,10):
                                            if g not in (a,b,c,d,e,f):
                                                for h in range(0,10):
                                                    if h not in (a,b,c,d,e,f,g):
                                                        for i in range(0,10):
                                                            if i not in (a,b,c,d,e,f,g,h):
                                                                for j in range(0,10):
                                                                    if j not in (a,b,c,d,e,f,g,h,i):
                                                                        z = 1000000000*a + 100000000*b + 10000000*c + 1000000*d + 100000*e + 10000*f + 1000*g + 100*h + 10*i + j
                                                                        m = m + 1
                                                                        if m == 1000000:
                                                                            print z
                                                                            done = True
                                                                    if done:
                                                                        break
                                                            if done:
                                                                break
                                                    if done:
                                                        break
                                            if done:
                                                break
                                    if done:
                                        break
                            if done:
                                break
                    if done:
                        break
            if done:
                break
    if done:
        break

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List