Equilibrium is not equal

It is not possible to run a python quiz program when "useranswer" is "correctanswer" if the loop does not run as expected and claims they are not equal even when they are. I'm wondering if this is a problem when comparing strings stored in lists, but I'm really stuck on what to do to fix this. Any help would be much appreciated.

thank

import sys

print ("Game started")
questions = ["What does RAD stand for?",
            "Why is RAD faster than other development methods?",
            "Name one of the 3 requirements for a user friendly system",
            "What is an efficient design?",
            "What is the definition of a validation method?"]


answers = ["A - Random Applications Development, B - Recently Available Diskspace, C - Rapid Applications Development",
            "A - Prototyping is faster than creating a finished product, B - Through the use of CASE tools, C - As end user evaluates before the dev team",
            "A - Efficient design, B - Intonated design, C - Aesthetic design",
            "A - One which makes best use of available facilities, B - One which allows the user to input data accurately, C - One which the end user is comfortable with",
            "A - A rejection of data which occurs because input breaks predetermined criteria, B - A double entry of data to ensure it is accurate, C - An adaption to cope with a change external to the system"]

correctanswers = ["C", "B", "A", "A", "A"]
score = 0
lives = 4
z = 0

for i in range(len(questions)):
    if lives > 0:
        print (questions[z])
        print (answers[z])
        useranswer = (input("Please enter the correct answer letter here: "))
        correctanswer = correctanswers[z]
        if (useranswer) is (correctanswer):     //line im guessing the problem occurs on
            print("Correct, well done!")
            score = score + 1
        else:
            print("Incorrect, sorry. The correct answer was;  " + correctanswer)
            lives = lives - 1
            print("You have, " + str(lives) + " lives remaining")
        z = z + 1
    else:
        print("End of game, no lives remaining")
        sys.exit()

print("Well done, you scored" + int(score) + "//" + int(len(questions)))

      

+3


source to share


3 answers


For comparison use ==

:

if useranswer == correctanswer: 

      

Operator

is

performs identity comparison. And ==

, >

operators perform value comparisons, which is exactly what you need.




For two objects obj1

and obj2

:

obj1 is obj2  iff id(obj1) == id(obj2)  # iff means `if and only if`.

      

+8


source


Operators is

and is not

check the object identifier: x is y

there is true

if and only if x

and y

are the same object. While the operators <

, >

, ==

, >=

, <=

and !=

compare the values of two objects.

Therefore...

    if (useranswer) is (correctanswer):     //line im guessing the problem occurs on

      



It must be ...

    if useranswer == correctanswer:

      

Since you want to check if the user's answer matches the correct answer. They are not the same object in memory.

+7


source


is

the operator checks the identity of the object. Check the docs . I hope this SO thread might be helpful as well.

+1


source







All Articles