I am coding an shore line generator but something is not right with my code. I have a old code like this:
import random
import time
def land_errosion():
continent = [[0,0,0,0,0,0,0,0,0,0],
[0,'*','*','*','*','*','*','*','*',0],
[0,'*','*','*','*','*','*','*','*',0],
[0,'*','*','*','*','*','*','*','*',0],
[0,'*','*','*','*','*','*','*','*',0],
[0,'*','*','*','*','*','*','*','*',0],
[0,'*','*','*','*','*','*','*','*',0],
[0,'*','*','*','*','*','*','*','*',0],
[0,0,0,0,0,0,0,0,0,0]]
for row in range(8):
for square in range(9):
if continent[row][square] == 0:
if continent[row][square + 1] != 0:
continent[row][square + 1] = random.choice([0, continent[row][square+1]])
elif continent[row + 1][square] != 0:
continent[row + 1][square] = random.choice([0, continent[row+1][square]])
if continent[row][square] != 0:
if continent[row][square+1] == 0 or continent[row+1][square] == 0:
continent[row][square] = random.choice([0, continent[row][square]])
for row in range(9):
for square in range(10):
print(continent[row][square], end=' ')
print()
print()
if __name__ == '__main__':
print(f"--- {t1/1000000000} seconds ---")
And this is the new code. The differences are the new one uses for loops to create land while the old one use fixed pattern:
import random
import time
def land_errosion():
continent = []
sea_n = []
for i in range(10):
sea_n.append(0)
land_row = [0]
for i in range(8):
land_row.append('*')
land_row.append(0)
continent.append(sea_n)
for i in range(8):
continent.append(land_row)
continent.append(sea_n)
for verti in range(9):
for hori in range(9):
if continent[verti][hori] == 0:
if continent[verti][hori + 1] != 0:
continent[verti][hori + 1] = random.choice([0, continent[verti][hori + 1]])
elif continent[verti + 1][hori] != 0:
continent[verti + 1][hori] = random.choice([0, continent[verti + 1][hori]])
if continent[verti][hori] != 0:
if continent[verti][hori + 1] == 0 or continent[verti + 1][hori] == 0:
continent[verti][hori] = random.choice([0, continent[verti][hori]])
for verti in range(10):
for hori in range(10):
print(continent[verti][hori], end=' ')
print()
print()
if __name__ == '__main__':
land_errosion()
I don’t change the main code for create shore line but the output is totally different:
the old code:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 * * 0 0 * * * 0
0 0 * * * * * * * 0
0 0 * * * * * * 0 0
0 0 0 * * * * * * 0
0 * * * * * * * * 0
0 0 * 0 * * * * 0 0
0 0 0 0 0 0 0 0 0 0
the new code:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
I don’t understand why, someone can point me the problems?
Khoa Tống is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1