python - 等于不等于

标签 python equals

无法使 python 测验程序运行,当“useranswer”为“correctanswer”时,如果循环无法正常工作并声明它们不相等,即使它们是相等的。我想知道这是否是比较列表中保存的字符串的问题,但我真的不知道该怎么做才能修复它。任何帮助将不胜感激。

谢谢

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)))

最佳答案

您应该使用== 进行比较:

if useranswer == correctanswer: 

is 运算符进行身份比较。而 ==> 运算符进行值比较,而这正是您所需要的。


对于两个对象,obj1obj2:

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

关于python - 等于不等于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14945630/

相关文章:

java - 如何实现 hamcrest 匹配器

java - 在compareTo()中使用equals()?

python - 使用 python 时无法在 opencv 中找到 ReleaseCapture?

java - 为什么 Boolean 类中重写的 equals(Object) 方法不需要 boolean/Boolean 作为参数

c# - 有完整的IEquatable实现引用吗?

python - Django教程: where does question_id come from?

java - 如果 equals(null) 抛出 NullPointerException 是个坏主意吗?

python - 如何在 sqlalchemy 混合表达式中运行查询?

python - Eventhub 在 python 中以列表形式读取数据

python - 平滑数据并找到最大值