2017-11-08 9 views
-1

私のプログラムには小さな問題があります。私はPythonの新機能として、解決策を見つけるのに苦労しています。私はちょっと調べてみましたが、私が試したことから何もうまくいかなかったようです。私のコードは次の通りです:Pythonリストが追加されていない&変数が定義されていません

from PIL import Image 
import numpy as np 
from scipy import misc 
from pandas import * 

def maze_gen(): 
    ##Opens the Image 
    im_file = Image.open('15x15.png') 

    ##Reads the image pixel information 
    arr = misc.imread('15x15.png') 

    ##Sets the width, height and maze size variables 
    width = im_file.size[0] 
    height = im_file.size[1] 
    size = width * height 

    ##Defines the mapping, start and end points array 
    map = np.zeros([width, height], dtype=np.int) 
    start_pos = np.empty(shape=[1,2]) 
    end_pos = np.empty(shape=[1,2]) 

    ##Search image replacing white pixels with 1's in the mapping array 
    for x in range(width): 
     for y in range(height): 
      if 255 in arr[x,y]: 
       map[x,y] = 1 

    ##Find the start and end locations 
    for x in range(width): 
     if map[0,x] > 0: 
      start_pos = 0,x 

    for x in range(width): 
     if map[height -1 ,x] > 0: 
      end_pos = height -1 ,x 



    return width, height, size, map, start_pos, end_pos 

def check_neighbours(col, row): 
    width, height, size, map, start_pos, end_pos=maze_gen() 

    neighbours = list() 

    ##Debugging to check cell_current values are correct 
    print('col =', col) 
    print('row =', row) 

    if (col >= 0 and col < height and row >= 0 and row < width): 
      neighbours.append((col, row)) 
      print('Neighbours', neighbours, '\n') 

    if (len(neighbours) > 0): 
     return neighbours 
    else: 
     return None 

def solver(): 
    width, height, size, map, start_pos, end_pos=maze_gen() 

    ##Sets cell_current to the starting position  
    cell_current = start_pos 

    path = list() 
    path.append((cell_current)) 

    check_neighbours(cell_current[0]-1, cell_current[1]) ##Top neighbour 
    check_neighbours(cell_current[0], cell_current[1]+1) ##Right neighbour 
    check_neighbours(cell_current[0]+1, cell_current[1]) ##Bottom neighbour 
    check_neighbours(cell_current[0], cell_current[1]-1) ##Left neighbour 

    print('Neighbours in Solver', neighbours, '\n') 

def debug(): 
    width, height, size, map, start_pos, end_pos=maze_gen() 

    ##Prints maze information for debugging 
    print ('Maze width:', width) 
    print ('Maze height:', height) 
    print ('Maze size:', size, '\n') 

    ##Print Start and End points 
    print ('Start Point') 
    print (start_pos, '\n') 
    print ('End Point') 
    print (end_pos, '\n') 

    ##Prints mapping array for debugging 
    print ('Mapping Array') 
    print (DataFrame(map), '\n') 

if (__name__ == "__main__"): 
    solver() 

私が取り組んでいる問題は、ifステートメントの実行が成功するたびにリストの隣人が追加されないことです。

COL = -1行= 1つのネイバー[]

COL = 0、行= 2つのネイバー[(0,2)]

COL = 1行=代わりに次の読み出しを取得します1人の隣人[(1,1)]

COL = 0、行= 0ネイバー[(0,0)]

この問題のすべてのヘルプは素晴らしいだろう。私は非常にPythonには非常に愚かな間違いや私はコードを良くするために変更することができますので、私は非常に新しいことを念頭に置いてください!あなたはあなたのコード内の2つの変更を加える必要はあり

+2

「neighbours」が定義されていないというエラーが表示されます。なぜ/それはどこにあるべきだと思いますか? – Julien

+0

私の理解からは、print行が呼び出される前に呼び出されるcheck_neighbours関数の中で定義されています。 –

+0

はい、それはその機能にとってローカルです。あなたは可変範囲についてお読みください。 – Julien

答えて

0

  1. 隣人グローバル変数を作成します。

  2. neighbours.append((COL、行))を交換neighbours.append([COL、行])。これは、変数またはリストを既存のリストにのみ追加できるためです。

希望します。

+0

#2は完全に間違っています。あなたは 'list'に何かを追加することができます。'tuple'を追加することは、他の型を追加することと同じであり、' tuple'を使うことは、後でシーケンスを変更する必要がないときにメモリとスピードの方が効率的です(Pythonは短い 'これらは暗黙のうちに暗黙的に使用されているため、明示的に軽量擬似オブジェクトとして使用されているためです)。 – ShadowRanger

関連する問題