Search code examples
pythonwhile-loopdo-while

Hi, I'm a beginner coder. I'm facing a problem of figuring out how do I make my program read 2 of while loops in order to function properly


while userInput in op and userInput != "q":
    score += 1
    no_words += 1
    userInput = input((str(no_words)) + ". ").lower()

while userInput not in op and userInput != "q":
    score += 0
    no_words += 0
    print("Oops! Invalid input")
    userInput = input((str(no_words)) + ". ").lower()

I expect when the user gives an input, my program will read through both these while loops each time in order to provide a correct output. ( I'm building a game which the users will need to list out as many words they can base on the origin word.)

for example: Extreme

  1. tree
  2. meet
  3. ...
  4. ...

Goal of the game: The more words user able to give, the higher the score will be.


Solution

  • What you want to do is actually nest the condition within the loop.

    Also, you'll never need to hardcode in "+ 0" to anything. if it doesn't change, you can simply leave out the line.

    
    userInput = None
    while userInput != 'q':
        userInput = input((str(no_words)) + ". ").lower()
        if userInput in op:
           score += 1
           no_words += 1
        else:
           print('Oops! Invalid Input')
     
    
    
    

    Edit: The important takeaway is that you can't run through two different while loops at the same time; the first one ends by the time the second one starts, until you get to some more advanced techniques. The solution is to figure out how to use a single while loop to access all the different paths of what could be happening at that moment.