2016-09-24 2 views
1

방금 ​​Python을 배우기 시작했으며 코드가 작동하는 동안 코드 줄을 줄이려고합니다. 목록 방법을 사용할 생각 이었지만 실제로 해결책을 찾지 못했습니다. 미리 검색해 보았지만 광산과 관련된 것을 찾을 수 없었습니다.Python - 긴 if-elif 코드 줄 줄임

내 코드의 경우 한 공간에서 다른 공간으로 이동하는 것과 같습니다. 가능한 움직임은 지시에 의해 결정됩니다. 요점의 움직임을 결정하기 위해 내가 한 일은 userPoint (요점을 결정하는 부분)을 할당하는 것이 었습니다. 이동하려면 공백에 설정된 조건 (userInput.upper()으로 표시되는 사용 가능한 유일한 방향)을 충족시켜야합니다. 그렇지 않으면 입력이 유효하지 않은 것으로 이동하고 인쇄하지 않습니다.

if userInput.upper() == 'QUIT': 
    break 
elif userPoint == 0 and userInput.upper() =='EAST': 
    userPoint = 1 
elif userPoint == 1 and userInput.upper() == 'WEST': 
    userPoint = 0 
elif userPoint == 1 and userInput.upper() == 'EAST': 
    userPoint = 2 
elif userPoint == 1 and userInput.upper() == 'SOUTH': 
    userPoint = 4 
elif userPoint == 2 and userInput.upper() == 'WEST': 
    userPoint = 1 
elif userPoint == 3 and userInput.upper() == 'SOUTH': 
    userPoint = 6 
elif userPoint == 4 and userInput.upper() == 'NORTH': 
    userPoint = 1 
elif userPoint == 4 and userInput.upper() == 'EAST': 
    userPoint = 5 
elif userPoint == 5 and userInput.upper() == 'WEST': 
    userPoint = 4 
elif userPoint == 5 and userInput.upper() == 'SOUTH': 
    userPoint = 8 
elif userPoint == 6 and userInput.upper() == 'NORTH': 
    userPoint = 3 
elif userPoint == 6 and userInput.upper() == 'EAST': 
    userPoint = 7 
elif userPoint == 7 and userInput.upper() == 'WEST': 
    userPoint = 6 
elif userPoint == 7 and userInput.upper() == 'EAST': 
    userPoint = 8 
elif userPoint == 8 and userInput.upper() == 'WEST': 
    userPoint = 7 
elif userPoint == 8 and userInput.upper() =='NORTH': 
    userPoint = 5 
else: 
    print('Please input a valid direction.\n') 

도움 주셔서 감사합니다.

답변

0

나는 사전 매핑을 userInput 동을 때마다, 효과가 userDirection에 하나를 추가하는 것입니다이

mapping = { 
    0 : { 
     "EAST" : 1 
    }, 
    1 : { 
     "EAST": 2, 
     "WEST": 1, 
     "SOUTH": 4 
    } 
} 


if userInput.upper() == 'QUIT': 
    break 
else: 
    userInput = mapping[userPoint][userInput.upper()] 
0

공지 사항 등의 뭔가를 만들어 그것을 할 수 있습니다.

userInput을 테스트하고 userDirection을 계산하여 긴 줄을 줄일 수 있습니다.

if userInput.upper == "EAST": 
    userDirection += 1 
elif userInput.upper == "SOUTH": 
    userDirection += 3 
elif # etc. 
0

사용자가 다음과 같습니다 미로의 종류를 통해 이동하는 것 같다 :

2--1--0 
    | 
5--4 3 
|  | 
8--7--6 

우리는 세트의 목록으로 유효한 연결을 저장할 수 있습니다. 예를 들어, {0, 1}은 0에서 1 및 1에서 0으로 연결되는 연결을 나타냅니다. 집합에는 순서가 없으므로 {0, 1}{1, 0}은 같은 것입니다.

if userInput.upper() == 'QUIT': 
    break 

connections = [{0,1}, {1,2}, {1,4}, {3,6}, {4,5}, {5,8}, {6,7}, {7,8}] 

if userInput.upper() == 'EAST': 
    newUserPoint = userPoint + 1 
elif userInput.upper() == 'WEST': 
    newUserPoint = userPoint - 1 
elif userInput.upper() == 'SOUTH': 
    newUserPoint = userPoint + 3 
elif userInput.upper() == 'NORTH': 
    newUserPoint = userPoint - 3 
else: 
    newUserPoint = None 

movementIsValid = {userPoint, newUserPoint} in connections 
if movementIsValid: 
    userPoint = newUserPoint 
else: 
    print('Please input a valid direction.\n') 

는 튜토리얼 ( lists, sets)를 확인하실 수 있습니다, 목록 및 세트를 사용하는 방법에 대한 자세한 내용을 보려면. 위의 코드에서 새 포인트를 계산 한 다음 이전 포인트에서 새 포인트로의 연결이 있는지 확인합니다.

새로운 점을 계산할 때 제거 할 수있는 반복이 아직 있습니다.

if userInput.upper() == 'QUIT': 
    break 

directions = {'EAST': 1, 'WEST': -1, 'SOUTH': 3, 'NORTH': -3} 
connections = [{0,1}, {1,2}, {1,4}, {3,6}, {4,5}, {5,8}, {6,7}, {7,8}] 

if userInput.upper() in directions: 
    userDirection = directions[userInput.upper()] 
    newUserPoint = userPoint + userDirection 
else: 
    newUserPoint = None 

movementIsValid = {userPoint, newUserPoint} in connections 
if movementIsValid: 
    userPoint = newUserPoint 
else: 
    print('Please input a valid direction.\n') 

이 해결 방법에서는 방향에 dictionary을 사용합니다.

새로운 개념의 개념을 많이 사용 했으므로 필자가 제공 한 링크에서 해당 개념을 읽을 수 있으며 어디서나 붙어 있으면 StackOverflow에서 새로운 질문을 할 수 있습니다.