Home > Blockchain >  How to make this stop printing correct when answer is incorrect
How to make this stop printing correct when answer is incorrect

Time:09-17

I have a script for a small quick trivia type questionnaire.

My issue is that the console keeps printing out "Correct" no matter what, even when I type the wrong answer.

I need it to answer even case insensitive is what I'm trying to achieve.

q1 = input("Whos one on the one dollar bill?")
answer1 = "George Washington"

if q1 == answer1 or answer1.lower():
    print("Correct!")
else:
    print("Incorrect.")

CodePudding user response:

I would recommend you review how to use logical operators.

Your code should look like this:

answer1 = "George Washington"

if q1 == answer1 or q1 == answer1.lower():
    print("Correct!")
else:
    print("Incorrect.")

Each logical statement must have its own conditional statement.

Edit: A better way to approach this is just to make both lower case. if q1.lower() == answer1.lower():

CodePudding user response:

In your case you have to compare twice correct answer with the input answer, it should be like that:

q1 = input("Whos one on the one dollar bill?")
answer1 = "George Washington"

if q1 == answer1 or q1 == answer1.lower():
    print("Correct!")
else:
    print("Incorrect.")

Or like that:

q1 = input("Whos one on the one dollar bill?")
answer1 = "George Washington"

if q1.capitalize() == answer1:
    print("Correct!")
else:
    print("Incorrect.")

CodePudding user response:

This is not how you use or in python. The condition you used here is equivalent to:
(q1 == answer1) or (answer1.lower()) which basically means "q1 is equal to answer1, or answer.lower() is not empty".
To achieve what you wanted, you should do this:

q1 = input("Whos one on the one dollar bill?")
answer1 = "George Washington"

if q1 == answer1 or q1 == answer1.lower():
    print("Correct!")
else:
    print("Incorrect.")

Or to make things simpler, just translate them both to lowercase, and then compare, like so:

q1 = input("Whos one on the one dollar bill?")
answer1 = "George Washington"

if q1.lower() == answer1.lower():
    print("Correct!")
else:
    print("Incorrect.")

(Note that the latter is not 100% the same as what you intended, but it would cover more cases, which I believe is what you want here. The condition now will cover all occasions in which the user has entered the text "George Washington", regardless of where he used lowercase or uppercase letters.)

  • Related