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

Question:

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

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

Asked By: Mathen

||

Answers:

I think you’re misunderstanding how to apply while loops. I think this solution would work in your case, however I don’t entirely understand exactly what you are doing as some variables here were left out from your code sample. The logic should at least work.

while userInput != 'q':
    userInput = input((str(no_words)) + ". ").lower()
    if userInput in op:
       score += 1
       no_words += 1
    else:
       score += 0
       no_words += 0
       
Answered By: rsenne

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.

Answered By: PatOConnor

Only 1 while-loop is needed, with if-conditions inside.
Also, I’ve added a list-comprehension to check that all letters are valid.
This game is quite fun.. try below code:

op = 'extreme'
print('Create as many words as possible (repeat letters are allowed) with this string:', op)
score = 0
inputList = []
userInput = ''

while userInput != 'q':
    userInput = input('Words created {}:'.format(inputList)).lower()
    if all([e in list(op) for e in list(userInput)]) and userInput not in inputList:
        score += 1
        inputList.append(userInput)
    elif userInput != 'q':
        print('Oops! Invalid input')
    
print('Your final score:', score)

Expected output

Create as many words as possible (repeat letters are allowed) with this string: extreme
Words created []: met
Words created ['met']: met
Oops! Invalid input
Words created ['met']: tree
Words created ['met', 'tree']: team
Oops! Invalid input
Words created ['met', 'tree']: q
Your final score: 2
Answered By: danPho
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.