Jump to content
  • Advertisement
Sign in to follow this  
Gallivan

While Loop In Python

This topic is 4074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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:

for i in range(mapCols):
for j in range(mapRows):
print "[%s]" %(theChar),

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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:

mapRows = 0
mapCols = 0
theChar = "-"

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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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".

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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!