While Loop In Python
I've been trying to create a tile generator with little success.
Here's my code:
http://rafb.net/p/htu1lZ94.html
The problem is that when it reachs Line 18 it continues to increment that variable until mapColsCheck == mapCols. I want it to increment once, return to the imbedded while statement and repeat that process until mapColsCheck == mapCols.
What am I doing wrong?
When the inner while loop terminates for the first time mapRowsCheck == mapRows. The value remains unchanged in the next iteration and the loop isn’t executed. You have to reset the value of mapRowsCheck every iteration.
It’s easier using the for loop:
It’s easier using the for loop:
for i in range(mapCols): for j in range(mapRows): print "[%s]" %(theChar),
Although you can use while loops for this, you might want to use for loops instead:
for mapColsCheck in range(mapCols): for mapRowsCheck in range(mapRows): print "[%s]" %(theChar)
Your problem is that you need to reset mapColsCheck to 0 after printing each row. However, a more pythonic apporach would probably look more like:
Edit: Beaten to it...twice [rolleyes]
mapRows = 0mapCols = 0theChar = "-" mapRows = input("Number of rows in generation: ")mapCols = input("Number of columns in generation: ") raw_input('Hit any key to generate.') for y in range( mapRows ): for x in range( mapCols ): print "[%s]" % theChar # If you want linebreaks between rows #print "\n" print "Completed."
Edit: Beaten to it...twice [rolleyes]
Thanks guys. I still have a problem though:
Instead of a 3 x 3 forming this:
[x][x][x]
[x][x][x]
[x][x][x]
With a comma it forms a vertical line, and without it forms a horizontal. Is there any easy way to determine line breaks?
Instead of a 3 x 3 forming this:
[x][x][x]
[x][x][x]
[x][x][x]
With a comma it forms a vertical line, and without it forms a horizontal. Is there any easy way to determine line breaks?
I fixed it. :D
I'm actually quite proud of myself for figuring out that by using:
if mapRowsCheck == (mapRows - 1):
print
else:
print "[%s - %s]" %(mapRowsCheck, mapColsCheck),
The thing would loop and create squares.
I'm actually quite proud of myself for figuring out that by using:
if mapRowsCheck == (mapRows - 1):
else:
print "[%s - %s]" %(mapRowsCheck, mapColsCheck),
The thing would loop and create squares.
You can also add a line at the end of the outer loop that print a newline:
for i in range(mapCols): for j in range(mapRows): print "[%s]" %(theChar), print
Is print the best way to initiate a new line?
[Edited by - Gallivan on June 21, 2007 11:03:20 PM]
[Edited by - Gallivan on June 21, 2007 11:03:20 PM]
An alternative would be build a string with newlines ('\n') embedded in it and then print the whole thing after the loop has finished. It's pretty much the same thing, only "buffered".
Of course, I could go nuts and write it in one line:
[disturbed]
buffer = '''for i in range(mapCols): for j in range(mapRows): buffer += '['+theChar+']' buffer += '\n'print buffer
Of course, I could go nuts and write it in one line:
print (('['+theChar+']')*mapCols+'\n')*mapRows
[disturbed]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement