Home > OS >  I want to make a loop which stucks user till the correct word entered, but when the correct word ent
I want to make a loop which stucks user till the correct word entered, but when the correct word ent

Time:04-03

I want to make a loop which stucks user till the correct word entered, but when the correct word entered it doesn't show the proper output. Here what i have done

word = "High Sir"
        Letter = input("How is the Josh! \n")
        while Letter!= word:
            if Letter == word:
                Letter = ("Well Done! You are selected for next stage.")
            else:
                Letter = input("Bullshit, Enter Again\n")

CodePudding user response:

It is not working because your condition for the loop is while Letter != word which means that you only enter the loop when Letter and word are NOT equal. Hence, when the user enters the correct word, Letter and word become equal and the loop condition fails which is why it does not enter the loop. You want something like this:

required_word = "High Sir"

while True:
    user_word = input("How is the Josh! \n")
    
    if user_word == required_word :
        print("Well Done! You are selected for next stage.")
        break
    else:
        print("Incorrect word! Try again")
  • Related