0

I want to ask my user a yes or no question over and over again and keep giving the response 'Correct' or 'Incorrect' back to the user.

print("Answer Yes to this Question")
check = str(input())

while check == ['yes', 'y', 'Yes', 'Y']:
    print('Correct!')
    break
else:
    print('Incorrect')
    check = str(input('Try Again'))

I don't want the code to exit or finish, the user should be able to continously answer questions. Currently when I run the code, no matter what I do, the response I get back in terminal is 'Incorrect' after which I get to give one more input and then the code exits. What did I do wrong?

Amrit
  • 17
  • 4
  • You can't compare a string to a list... Can you take it from there? – Aleksa Feb 03 '20 at 03:22
  • 1
    You should learn the basics of python first. There are many issues in your code. To check if a sting is in a list, you need to use `if item in list`. And to take inputs indefinitely, you need an infinite loop. – Sнаđошƒаӽ Feb 03 '20 at 03:23
  • Ah yes, they are two different data types, what if I use 'While check in' ? – Amrit Feb 03 '20 at 03:26
  • How would I use an infinite loop to keep this code running? – Amrit Feb 03 '20 at 03:28
  • 1
    Does this answer your question? [How to run the Python program forever?](https://stackoverflow.com/questions/20170251/how-to-run-the-python-program-forever) – Selcuk Feb 03 '20 at 03:29
  • Theanks @Selcuk that post helped me write this `while (check in ['yes', 'y', 'Yes', 'Y'])`. Now my code runs continually while the statement is true but the else statement causes the code to stop running. How do I prevent this from happening? – Amrit Feb 03 '20 at 03:34
  • @Amrit `while..else` is **not** what you think and not used that often. What you really wanted to use was an `if..else` clause. Read a Python tutorial first that will guide you through the basics. – Selcuk Feb 03 '20 at 03:37

1 Answers1

1

Try this

while True:
  print("Answer Yes to this Question")
  check = str(input())
  if check.lower() in ['yes', 'y']:
    print('Correct!')
  else:
    print("Incorrect, try again")
Keelan Pool
  • 177
  • 1
  • 15