2017-03-19 4 views
-3

私はいくつかの質問をyesまたはnoの回答で書こうとしており、別の文字列を入力するとユーザーにyesまたはnoを入力するようにしたいと思いますまたはいいえ)。初心者のPython - 真のループチェック中はいいいえ回答

私はWhile Trueループを使用しましたが、これを実行するたびにq1に戻ります。

while True: 
q1 = input("Switch on, yes or no") 
q1= q1.title() 

if q1 == "No": 
    print("Charge your battery") 
    break 

elif q1 == "Yes": 
    q2 = input("Screen working?") 
    q2 = q2.title() 
    if q2 == "No": 
     print("replace screen") 
     break 

    elif q2 == "Yes": 
     q3 = input("Ring people?") 
     q3 = q3.title() 
     if q3 == "No": 
      print("Check your connecting to your network") 
      break 

     elif q3 == "Yes": 
      print("Not sure") 
      break 

print("Thanks for using")  
+2

インデントの問題は非常に多いです。 – ForceBru

+0

[有効な応答を返すまでユーザーに入力を求める](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-応答) – tripleee

答えて

1

あなたは二つのことを修正する必要がありますコードを動作させるためには:

  • インデント
  • は(breakcontinuepassの間で異なる程度見hereを取る)continue
  • breakを置き換えます

次のバージョンは仕事:

while True: 
    q1 = input("Switch on, yes or no") 
    q1= q1.title() 

    if q1 == "No": 
     print("Charge your battery") 
     continue 

    elif q1 == "Yes": 
     q2 = input("Screen working?") 
     q2 = q2.title() 
     if q2 == "No": 
      print("replace screen") 
      continue 

     elif q2 == "Yes": 
      q3 = input("Ring people?") 
      q3 = q3.title() 
      if q3 == "No": 
       print("Check your connecting to your network") 
       continue 

      elif q3 == "Yes": 
       print("Not sure") 
       continue 

print("Thanks for using")  
関連する問題