0

What my goal is: I am currently trying to build a simple troubleshooting program. the variable, current_question is meant to update as the value of each input/question along a nested if statement. This is so the code can identify which question it is currently asking. As a result, I can loop back to the current question in case the user enters the wrong data (not yes and not no)

The issue However, when I test the code and it continues to the second question, this variable is not updated to questions 2,3,4 BUT the loop works only with the first question whenever I type in the wrong data. I have been trying to solve this for a long time but still haven't reached a solution, so I thought this would be a good place to start.

The code!

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)
miradulo
  • 28,857
  • 6
  • 80
  • 93
sharifee
  • 33
  • 5

2 Answers2

0

If your intention is to loop for every possible question if-elif won't do. You must use individual if for every possible scenario, and must remove the break statement.

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..
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
lumee
  • 613
  • 1
  • 5
  • 15
  • I would propose to use `if q1 in ('yes', 'Yes'):` or even `if q1.lower() == 'yes':` etc. – Alfe May 18 '16 at 16:13
0

Your second while is reached when the question-loop has ended - caused by any reason. After the user types in wrong data, the question is asked again and the program ended, because you don't tell python to resume the main loop.

Get rid of the current loops ans instead of the ifs use your loops there:

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":
    [...]

and so on

itpdg
  • 316
  • 1
  • 3
  • 10