2017-09-30 7 views
0

コードを2回貼り付けて行を追加することなく、 "if direction == north:"を "if direction = south"に接続する方法はありますか?Python 3のコードを接続する

direction = input('do you want to travel north or south?') 

if direction =='north': 
    print('You chose North') 
    print('An iron gate block your path') 
    print('You need a bronze key to open it') 
    print('You will have to go South') 

if direction == 'south': 
    print('You chose South') 
    time.sleep(1) 
    print('The path leads you to the Deep woods of Mirkwood') 
+0

南北よりも方向が北の場合は、異なる動作があります。共通の声明は1つしかありません。その理由でそれらを接続したいですか?または、何を達成したいですか? –

+0

コードを重複排除するほうがずっと簡単なので、接続したい –

+0

アクションが完全に異なる場合は、2つのifステートメントが必要です。 –

答えて

0

「接続する」という意味を定義する必要があります。おそらく、出力が特定のパスに対して提供するものの例を提供します。ユーザーが北狩りの後、再び方向を選択する場合の選択肢を置き、そうでない場合は、

print('You will have to go South') 
direction = 'south' 

:あなたは南のパスを表示したい場合は、彼らが北に選択した後

、あなたのような何かを追加することができます

while True: 
    direction = input('do you want to travel north or south?') 

    if direction == 'north': 
     #print statements 
     continue 

    if direction == 'south': 
     #print statements 
     break