Home > other >  Hi, I'm a beginner coder. I'm facing a problem of figuring out how do I make my program re
Hi, I'm a beginner coder. I'm facing a problem of figuring out how do I make my program re

Time:12-31

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.

CodePudding user response:

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.

CodePudding user response:

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
       

CodePudding user response:

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
  • Related