2016-05-18 18 views
0

私の目標は: 私は現在、簡単なトラブルシューティングプログラムを構築しようとしています。変数current_questionは、ネストされたif文に沿った各入力/質問の値として更新されることを意図しています。これは、コードが現在質問している質問を識別できるようにするためです。その結果、私は戻って、ユーザが間違ったデータを入力する場合には、現在の質問にループすることができます(ないそうではなくなし)python whileループ、変数current_questionが更新されていません

問題 しかし、私は、コードをテストし、それが2番目の質問への継続した場合、この変数は質問2,3,4に更新されませんが、私は間違ったデータを入力するたびに最初の質問だけでループが動作します。私はこれを長い間解決しようとしてきましたが、解決策にはまだ達していないので、これは始めるのに適していると思いました。

コード!あなたの意図はないだろう、すべての可能な質問if-elifのためのループにある場合

import time 
current_question =() 
i = 1 
while i <= 4: 
    q1 = str(input('Is your phone wet? ')).lower() 
    current_question = q1 
    i = i + 1 
    if q1 == 'yes' or q1 == 'Yes': 
     print('dry it out') 
     break 
    elif q1 == 'no' or q1 == 'No': 
     q2 = str(input('is your phone cracked? ')).lower() 
     current_question = q2 
     i = i + 1 
     if q2 == 'yes' or q2 == 'Yes': 
      print('replace screen') 
      break 
     elif q2 == 'no' or q2 == 'No': 
      q3 = str(input('are you able to download apps/videos? ')).lower() 
      current_question = q3 
      i = i + 1 
      if q3 == 'yes' or q3 == 'Yes': 
       print('delete any apps or videos that you don\'t need') 
       break 
      elif q3 == 'no' or q3 == 'No': 
       q4 = str(input('Is your phone unresponsive? ')).lower() 
       current_question = q4 
       i = i + 1 
       if q4 == 'yes' or q4 == 'Yes': 
        print('restart it') 
        break 
       elif q4 == 'no' or q4 == 'No': 
        print('contact the supplier') 
        break 
while current_question != 'yes' and current_question != 'no': 
    print('try again') 
    time.sleep(1) 
    print(current_question) 
+0

にユーザーがあなたを与える応答を 'current_question'変数を設定している、ありますあなたは何をするつもりでしたか? –

+0

あなたの最後のwhileループでも 'current_question'の値を決して変更していないので、無限ループです – Keiwan

+0

あなたはあなたがどのような質問をしているか追跡する方法を探しているかもしれません(http: /stackoverflow.com/a/18863481/5827215) –

答えて

0

質問ループが終了したときに何らかの理由で2番目の時間に達しました。ユーザーが間違ったデータを入力すると、pythonにメインループを再開するように指示していないため、質問に再び尋ねられ、プログラムが終了しました。

があなたのループを使う代わりに、IFSの電流ループのANSを取り除く:

q1 = str(input('Is your phone wet? ')).lower() 
while q1 != "yes" and q1 != "Yes" and q1 != "no" and q1 != "No": 
    q1 = str(input('Is your phone wet? ')).lower() 
if q1 == 'yes' or q1 == 'Yes': 
    print('dry it out') 
elif q1 == 'no' or q1 == 'No': 
    q2 = str(input('is your phone cracked? ')).lower() 
    while q2 != "yes" and q2 != "Yes" and q2 != "no" and q2 != "No": 
    [...] 

ので

+0

皆さん助けてくれてありがとう!私はそれを持っている、多くの感謝:) – sharifee

0

。可能なシナリオごとに個別のifを使用し、breakステートメントを削除する必要があります。

if q1 == 'yes' or q1 == 'Yes': 
     print('dry it out') 
if q1 == 'no' or q1 == 'No': 
     q2 = str(input('is your phone cracked? ')).lower() 
     current_question = q2 
     i = i + 1 
if q2 == 'yes' or q2 == 'Yes': 
     print('replace screen') 
if q2 == 'no' or q2 == 'No': 
      q3 = str(input('are you able to download apps/videos? ')).lower() 
      current_question = q3 
      i = i + 1 
etc.. 
+0

q1を( 'yes'、 'Yes'): 'または' q1.lower()== 'yes': 'などのように使用することを提案します。 – Alfe

関連する問題