Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Simple for loop question
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

 Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
 01-30-2008, 05:14 AM PM User | #1 MindTheGap New Coder   Join Date: Jul 2007 Posts: 67 Thanks: 0 Thanked 0 Times in 0 Posts Simple for loop question I"m making a sudoku solver in python. Here's my code: Code: ```from numpy import * def isValid(grid): count = 0 for x in range(0,9): for y in range(0,9): if grid[x][y] == 0: # tell it to skip rest of code in function and keep going through for loop a, b = x, y x = 0 for i in range(0,9): if grid[a][b] == grid[x+i][b]: count += 1 if count != 1: return 2 count = 0 return 1 def isDone(grid): total = 0 for r in range(0,9): for c in range(0,9): total += grid[r][c] if total != 45: return 2 total = 0 for c in range(0,9): for r in range(0,9): total += grid[r][c] if total != 45: return 2 total = 0 x, y = 1, 1 for a in range(0,9): if y == 10: y = 1 x += 3 total += grid[x][y]+grid[x-1][y-1]+grid[x-1][y]+grid[x-1][y+1]+grid[x][y-1]+grid[x][y+1]+grid[x+1][y-1]+grid[x+1][y]+grid[x+1][y+1] if total != 45: return 2 y += 3 total = 0 return 1 def solve(grid): if isValid(grid) == 2: return 2 if isDone(grid) == 1: return 1 for i in range(0,9): for j in range(0,9): if grid[i][j] == 0: for g in range(1,10): grid[i][j] = g if solve(grid) == 1: return 1 grid[i][j] = 0 return 2 def display(g): for i in range(0,9): print str(g[i][0])+" "+str(g[i][1])+" "+str(g[i][2])+" "+str(g[i][3])+" "+str(g[i][4])+" "+str(g[i][5])+ " "+str(g[i][6])+" "+str(g[i][7])+" "+str(g[i][8]) grid = arange(81).reshape(9,9) print "Time to input the grid! Input 0 for blank space." # ENTER YOUR SUDOKU PUZZLE GRID HERE: # # USE ZEROES FOR BLANK SPACES # # A SAMPLE PUZZLE IS GIVEN: # grid = [ [5,3,4,6,7,8,9,1,2], [6,7,2,1,9,5,3,4,8], [1,9,8,3,4,2,5,6,7], [8,0,0,0,6,0,0,0,3], [4,0,0,8,0,3,0,0,1], [7,0,0,0,2,0,0,0,6], [0,6,0,0,0,0,2,8,0], [0,0,0,4,1,9,0,0,5], [0,0,0,0,8,0,0,7,9] ] ####################################### print "Here is what the Sudoku grid looks like: " display(grid) if isValid(grid) == 1: solve(grid) print "Here is the solved Sudoku Puzzle!: " print "----------------------------------" display(grid) else: print "not valid"``` If you look at my isValid function I wrote out what I want to do. Is there any statement that can do this? I've tried continue but I got stuck in an infiinite loop. Help please! Thanks. YOU NEED NUMPY TO RUN THIS....i think.
01-30-2008, 06:03 AM   PM User | #2
shyam
Senior Coder

Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
Quote:
 Originally Posted by MindTheGap Code: ```from numpy import * def isValid(grid): count = 0 for x in range(0,9): for y in range(0,9): if grid[x][y] == 0: # tell it to skip rest of code in function and keep going through for loop a, b = x, y x = 0 for i in range(0,9): if grid[a][b] == grid[x+i][b]: count += 1 if count != 1: return 2 count = 0 return 1```
you are reassigning the loop variable withing the loop...which is the reason for ur infinite loop
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

 01-31-2008, 12:16 AM PM User | #3 MindTheGap New Coder   Join Date: Jul 2007 Posts: 67 Thanks: 0 Thanked 0 Times in 0 Posts Thanks! I tried to fix it but did not suceed. Why does this cause an infinite loop: Code: ```def isValid(grid): count = 0 for x in range(0,9): for y in range(0,9): if grid[x][y] != 0: print "infty" for i in range(0,9): if grid[x][y] == grid[i][y]: count += 1 if count != 1: return 2 count = 0 return 1```

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 03:21 PM.

 AdvertisementLog in to turn off these ads.