Reputation: 39
Can't get python quiz program to work, when the 'useranswer' is 'correctanswer' if loop doesn't work properly and states they aren't equal even when they are. I'm wondering if this is a problem with comparing strings saved in lists, but I am really stuck for what to do to fix it. Any help would be much appreciated.
Thank you
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's 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)))
Upvotes: 3
Views: 249
Reputation: 213243
You should use ==
for comparison:
if useranswer == correctanswer:
is
operator does identity comparison. And ==
, >
, operators do value comparison, and that is what you need.
For two objects, obj1
and obj2
:
obj1 is obj2 iff id(obj1) == id(obj2) # iff means `if and only if`.
Upvotes: 8
Reputation: 9458
is
operator tests object identity. Check the docs. I hope this SO thread might also be helpful.
Upvotes: 1
Reputation: 141810
The operators is
and is not
test for object identity: x is y
is true
if and only if x
and y
are the same object. Whereas the operators <
, >
, ==
, >=
, <=
, and !=
compare the values of two objects.
Therefore...
if (useranswer) is (correctanswer): //line im guessing the problem occurs on
Should be...
if useranswer == correctanswer:
Since you want to check whether the user's answer matches the correct answer. They're not the same object in memory.
Upvotes: 7