2016-09-24 11 views
1

私はちょうどPythonを学び始めました。私のコードが動作している間、私はコード行を減らしたいと思います。私はlistメソッドを使うことを考えていましたが、実際には解決策を思いつくことができませんでした。私は事前に検索してみましたが、私に関連するものは見つけられませんでした。Python - 長いif-elifコード行を減らす

私のコードでは、1つのスペースを別のスペースに移動するような感じです。可能な動きは方向によって決定されます。私がポイントの動きを決定するために行ったことは、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に1を追加することであることを、この

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') 

、チュートリアル(listssets)をチェックアウトすることができます。上記のコードでは、新しいポイントを計算し、古いポイントから新しいポイントへの接続があるかどうかを確認します。

新しいポイントを計算するときに取り除くことができるいくつかの繰り返しがあります。

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を使用します。

私はあなたのために新しいかもしれない多くの概念を使用しました。私が与えたリンクでそれらについて読むことができます。そして、あなたがどこにいてもスタックオーバーフローで新しい質問をすることができます。

関連する問題