2012-04-04 4 views
0

한 단계를 제외하고 숙제로 모든 것을했습니다.미로 탐색 (파이썬 2.7 사용)

나는 그것을 다른 방식으로했지만 어쨌든 정답을 주었다. 어쨌든, 나는 maz를 탐험해야한다. 그래서이 일의 일부 (Q COMMAND)를 제외하고 모든 것을 완전히 끝내고 모든 것을 완전히 끝내었다.

나는 문자열 방법을 사용할 필요가 .upper()

2.2.6 인터페이스 interact()가 도입에 설명 된대로 텍스트 기반의 사용자 인터페이스 를 denes 최상위 함수 인 최상위. 마무리 사각형이 발견 될 때 사용자가 종료하거나 을 누르면 상호 작용 기능이 종료됩니다.

def interact(): 
    mazefile = raw_input('Maze File: ') 
    maze = load_maze(mazefile) 
    position = (1, 1) 
    poshis = [position] 

    while True: 
     #print poshis, len(poshis) 
     print 'You are at position', position 
     command = raw_input('Command: ') 
     #print command 

     if command == '?': 
      print HELP 
     elif command == 'N' or command == 'E' or command == 'S'or command == 'W': 
      mov = move(maze, position, command) 
      if mov[0] == False: #invalid direction 
       print "You can't go in that direction" 
      elif mov[1] == True:#finished 
       print 'Congratulations - you made it!' 
       break 
      else: #can move, but not finished 
       position = mov[2] 
       poshis.append(position) 

     elif command == 'R': # reseting the maze to the first pos 
      position = (1, 1) 
      poshis = [position] 
     elif command == 'B': # back one move 
      if len(poshis) > 1: 
       poshis.pop() 
       position = poshis[-1] 

     elif command == 'L': # listing all possible leg dir 
      toggle = 0 
      result = '' 
      leg_list = get_legal_directions(maze, poshis[-1]) 
      for Legal in leg_list: 
       if toggle: 
        result += ', ' 
        result += Legal 
       else: 
        result += Legal 
       if not toggle: 
        toggle = 1 
      print result 

     elif command == 'Q': #quiting 
      m = raw_input('Are you sure you want to quit? [y] or n: ') 
      if m == 'y': 
       break 

      #print 'Are you sure you want to quit? [y] or n: ' 
      #if raw_input == 'y': 
       # break 

     else: #invalid input 
      print 'Invalid Command:', command 
+1

"왜 종료하지 않습니까?" 무슨 일이 일어날 것으로 예상됩니까? 파이썬 2.7이 중요한 이유는 무엇입니까? 마지막 부분을 해결하지 않은 방식에 대한 세부 정보를 게시 할 수 있습니까? 미로 파일의 두 줄 또는 세 줄을 게시 할 수 있습니까? – octopusgrabbus

답변

0

귀하의 질문은 특히 명확하지 않다, 그러나 나는 '문제는'사용자가 대신 "Y"의 "Y"를 대답한다면 그들이 하시겠습니까 여부를 묻는 경우이다 같은데요 종료하면 루프가 계속됩니다.

그게 문제가있는 경우, 당신은 단지 라인을 교체해야합니다 :

if m == 'y': 
    break 

과를 :

때문에 관계없이
if m.upper() == 'Y': 
    break 

사용자 유형 "Y"또는 "Y", 루프 여부 여전히 부셔 질 것입니다.

+0

하지만 선생님이 말씀 드린대로 이것을 사용해야합니다 : 상호 작용 기능의 사용자 입력은 대문자로 변환해야합니다. 사용자가 명령을 요청할 때 명령을 사용합니다. aluook (method.upper) eg.'asdf'.upper() - > 'ASDF' 또한 상호 작용을 종료 할 때 [y]는 종료가 기본 옵션임을 의미하므로 기본적으로 사용자가 'N'을 입력하면 종료해서는 안되며 다른 입력을 종료해야합니다. 예. '종료 하시겠습니까? [y] 또는 n : ''N '-> 종료하지 마십시오 '종료 하시겠습니까? [y] 또는 n : ''Y '-> 종료 '종료 하시겠습니까? [y] 또는 n : ''asdf '-> 종료 – Alzaeemah