0

I am relatively new to coding in python and haven't seen my question answered wherever I have looked on the internet. its a simple problem which I'm sure hopefully the pros can easily fix. I am trying to make a quiz with a series of true and false questions. I am trying to make a loop where if the user does not answer with either true or false, then the program asks the user to input the answer again as either true or false. So a simple loop where the user is asked the same question again if the user does not answer with either "true" or "false". I am experiencing problems where it keeps entering the loop even if the conditions don't meet. my code:

print('you will be asked a series of true or false questions. please answer accordingly with "true" or "false"')
print()

ans1=input("is the dodo bird extinct?: ").lower()

while ans1!=("true") and ("false"):
    ans1=input("please answer with either true or false. do dodo birds still exist?: ").lower()

    
if ans1==("false"):
    print("congratulations your answer is correct!")
    
if ans1==("true"):
    print("unfortunately your answer is incorrect. the answer is false. the dodo birds went extinct a long time ago.")        

If I enter "true" in the original input then it tells me the that my answer is incorrect as its supposed to:


you will be asked a series of true or false questions. please answer accordingly with "true" or "false"

is the dodo bird extinct?: true
unfortunately your answer is incorrect. the answer is false. the dodo birds went extinct a long time ago.

But if I put false then it sends me into the loop where instead it was supposed to tell me that my answer is correct:

you will be asked a series of true or false questions. please answer accordingly with "true" or "false"

is the dodo bird extinct?: false
please answer with either true or false. do dodo birds still exist?: 

But if I put true in the input within the while loop then it gives me the answer:

you will be asked a series of true or false questions. please answer accordingly with "true" or "false"

is the dodo bird extinct?: false
please answer with either true or false. do dodo birds still exist?: true
unfortunately your answer is incorrect. the answer is false. the dodo birds went extinct a long time ago.

But if I put false in, it doesn't break the loop and tell me that the answer is correct. instead it stays in the loop and keeps asking the same question:

you will be asked a series of true or false questions. please answer accordingly with "true" or "false"

is the dodo bird extinct?: false
please answer with either true or false. do dodo birds still exist?: false
please answer with either true or false. do dodo birds still exist?: 

How do I use the boolean operators to make it so that if either one of the statements don't meet the conditions then it breaks the loop or doesn't enter it in the first place.

Thicc
  • 1

1 Answers1

0

Ok so I spent 5 hours trying to solve this problem. Nothing had seemed to work but user "j1-lee" suggested me to do "ans1 != "true" and ans1 != "false":?". I had already tried this method and it didn't seem to work but when I decided to try it again because why not, IT WORKED. I am frustrated beyond belief on why this happened. it didn't happen before because even my friend came and tried to help me but couldn't using the same method. regardless, It seems that I had probably messed up something else before and have wasted hours of time due to my ignorance or something.

Thicc
  • 1