2017-05-03 3 views
-3

私のプログラミングクラスでは、単純なテキストアドベンチャースタイルのコードをランダム化する必要があります。私は基本を終えることができますが、最後に再試行を追加しようとすると、コードの先頭に戻るのではなく、繰り返し再試行するかどうかだけをユーザーに尋ねます。Pythonコードはループしません。

import random 

def main(): 

    cave1=('text1') 

    cave2=('text2') 

    #Radom assignment for caves 
    checkCave=random.randint(1,2) 

    #Text of situation 
    displayIntro='Two caves, one good, one bad' 
    print(displayIntro) 

    #input for cave choice. 
    chooseCave=int(input('Which cave do you enter?: (enter a 1 or 2): ')) 

    while chooseCave<1 or chooseCave>2: 
     print('Error: please enter a 1 or a 2.') 
    print() 

    #outcome 
    print('You slowly walk towards the cave.') 
    print() 

    if checkCave==1: 
     print(cave1) 
    else: 
     print(cave2) 
    print() 

    again='y' 
    while True: 
     while True: 
      again=input('Do you want to try again? y for yes, n to quit: ') 
      if again in ('y','n'): 
       break 
      print('Invalid input.') 
     if again=='y': 
      continue 
     else: 
      print('Goodbye') 
      break 

    return again 

main() 

助けていただければ幸いです。感謝! ありがとう!

+0

続きが問題を引き起こしています – Aditya

答えて

0

continueループ内で見つかったforループまたはwhileループの次の反復を開始することを意味します。外側のコードをループしたい場合は、外側のforループが必要です。

物事を関数に入れると、物事をより簡単にするのに役立ちます。

幸運を祈る!

関連する問題