22.7k views
2 votes
You're familiar with the story of Hansel and Gretel, so when you decide to go exploring you always make sure to keep track of where you've come from. Since you know how unreliable leaving a trail of breadcrumbs is, you decide to write a program to help you map out your trail. Your program should first read in the size of the grid that you will be exploring, and then read in directions: left,right, up or down. You always start exploring at the top left of your map. After every step you should print out the trail, reading in directions until a blank line is entered. Your program should work like this: Grid size: 3 Direction: right Direction: dowrn Direction: right .Xx You will never be directed off the edges of the map.

1 Answer

1 vote

Answer:

See explaination for the program code.

Step-by-step explanation:

size = int(input('Grid size: '))

table = []

for i in range(size):

row=[]

for j in range(size):

row.append('.')

table.append(row)

table[0][0] = 'x'

for row in table:

print(*row,sep='')

Direction = input('Direction: ')

while Direction:

if Direction == 'right':

for i in range(size):

for j in range(size-1):

if table[i][j] == 'x':

table[i][j+1] = 'x'

table[i-1][j+1] = '.'

break

elif Direction == 'down':

for i in range(size):

for j in range(size-1):

if table[j][i] == 'x':

table[j+1][i] = 'x'

table[j+1][i-1] = '.'

break

for row in table:

print(*row,sep='')

Direction = input('Direction: ')

User Gonjay
by
8.4k points