すみません、以下のコードで
もし"A"がheight+1かwidth+1にプリントされた時にプログラムが終了する様にするにはどうすれば良いでしょうか?
python
1import random 2import time 3 4# Ask the user to input how big the grid is. 5width = int(input("State the width of the grid: ")) 6height = int(input("State the height of the grid: ")) 7 8ant = ["A"] 9grid = {} 10previouspos = (-1, -1) 11 12# find the empty space in the grid. 13for y in range(height+1): 14 for x in range(width+1): 15 coordinate = (x, y) 16 grid[coordinate] = " " 17 18# Print the grid. 19def print_grid(): 20 for y in range(height): 21 for i in range(width): 22 print("+---", end="") 23 print("+") 24 for x in range(width): 25 if x == 0: 26 print("|", end="") 27 coordinate = (x, y) 28 print("", grid[coordinate], end=" |") 29 if y != height: 30 print("") 31 for i in range(width): 32 print("+---", end="") 33 print("+") 34 35def change_grid(): 36 # Place ant in the grid. Randomly. 37 place, ant_place = random.choice(list(grid.items())) 38 ant_place = random.choice(ant) 39 grid[place] = ant_place 40 41 42# Printing the grid and changing it every 1 seconds 43for turns in range(10): 44 print_grid() 45 time.sleep(1) 46 for y in range(height): 47 for x in range(width): 48 coordinate = (x, y) 49 if (previouspos == coordinate): 50 grid[coordinate] = " " 51 place, item = random.choice(list(grid.items())) 52 ant_place = random.choice(ant) 53 grid[place] = ant_place 54 previouspos = place 55 if turns != 4: 56 print() 57 58 59 60print_grid() 61
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。