Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's wrong?

Status
Not open for further replies.

eamc

Technical User
Sep 3, 2001
83
US
The following runs just one time out of five:

import math
for a1 in range (1,9,1):
for a2 in range (1,9,1):
for a3 in range (1,9,1):
for a4 in range (1,9,1):
for a5 in range (1,9,1):
for a6 in range (1,9,1):
for a7 in range (1,9,1):
for a8 in range (1,9,1):
for a9 in range (1,9,1):
if a1+a2+a3+a4+a5+a6+a7+a8+a9 == 11:
print a1,a2,a3,a4,a5,a6,a7,a8,a9

What am I doing wrong?

Edwin M.
 
runs fine over here
very slowly however

can you give more info re the problems you are having?
how are you running the script?
 
Thanks Justin. I enter the lines you see in the Python Idle Shell and press [Enter]. When it works, I get the blue listings of the 9 numbers, indeed slowly, but something always starts within 15 seconds. When it does not work, it prints >>> and then nothing. I am trying to write a Sudoku program, which would require 26 more blocks like this one - looks hopeless. I guess it is not possible to write just one line for a1 to a9 instead of 9, i.e. something like [for a1,a2 in range so-and-so]. In practice, some 2-4 of the lines would have single values, not a range. However, I am a rank beginner. It seems to me that this sort of thing ran much faster in Turbo Pascal, and the fault may be with Windows XP, even though I have 2 GB RAM.

Edwin M.
 
I don't know why it's not working in the idle shell, I would suspect a problem with the cut and paste. When I paste it into a python shell under linux it runs fine. Get the cut and paste our of the mix and put it all in a file and run it.

You realize that there's no reason to go range(1,9), because any time any of your counters goes above 3, the sum will always be > 11. You may as well do range(1,3), it'll run faster.

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top