需要检查测验中的答案是否正确(Python)

需要检查测验中的答案是否正确(Python),python,dictionary,Python,Dictionary,所以我有一本关于立法者和他们所属政党的字典。五个问题被输出为随机名称和参与方,输入为Y或N。我现在需要弄清楚如何检查它是否正确,但我被难倒了。 守则: from random import * legislators = { "Tsang Yok-sing" : "DAB", "Albert Ho" : "Democratic", "Lee Cheuk-yan" : "Labour", "James To" : "Democratic", "Chan Kam-lam" : "DAB", "La

所以我有一本关于立法者和他们所属政党的字典。五个问题被输出为随机名称和参与方,输入为Y或N。我现在需要弄清楚如何检查它是否正确,但我被难倒了。 守则:

from random import *

legislators = { "Tsang Yok-sing" : "DAB", "Albert Ho" :
"Democratic", "Lee Cheuk-yan" : "Labour", "James To" :
"Democratic", "Chan Kam-lam" : "DAB", "Lau Wong-fat" :
"Economic Synergy", "Emily Lau" : "Democratic" }

names = list(legislators.keys())
parties = list(legislators.values())

numberCorrect = 0

for i in range(0, 5):
    name = names[randrange(len(names))]
    party = parties[randrange(len(parties))]
    ans = input("Does "+name+" belong to "+party+" (Y/N)?\n")

只是需要得到任何关于从哪里开始的建议。非常感谢

由于您将原件存储在dict中,您只需检查立法者[姓名]==政党

是否添加了代码示例。您的代码硬编码为大写Y/N

输入之前
计算随机组合的有效性

if legislators[name] == party:
    valid = "Y"
else:
    valid = "N"
现在,在
输入后
需要执行以下操作:

if ans==valid:
   if valid == 'N':
      print "Yes, the member does not belong to that party."
   else:
      print "Yes, the member belongs to that party."
else:
    print "Sorry, your answer is wrong."

您的程序还有一个问题:

选择一个随机成员和一个随机政党,该政党出错的概率为34/49或大约70%,因此始终回答“n”将给出3.47/5的平均正确分数

我们可以这样解决这个问题:

# 50% chance of using the correct party,
# 50% chance of using any other party
test_party = party if random() < 0.5 else choice(other_parties[party])

好的,我试试看。谢谢,不太好。如果用户输入了
n
,那么检查
立法者[姓名]
是否等于
政党
将得到他想要的相反答案。你是指“n”还是“n”。您已经预先定义了答案。与之相比。这似乎是正确的。谢谢,我是python新手,所以我知道的不多。通过检查立法者['name']==党,你已经知道了预期的答案。现在你用它来验证用户的条件。这太完美了!我对你为帮助我所付出的努力感激不尽!
# 50% chance of using the correct party,
# 50% chance of using any other party
test_party = party if random() < 0.5 else choice(other_parties[party])
from random import choice, random

NUM_QUESTIONS = 5

def get_yn(prompt, error_message=None, yes_values={'', 'y', 'yes'}, no_values={'n', 'no'}):
    """
    Prompt repeatedly for user input until a yes_value or no_value is entered
    """
    while True:
        result = input(prompt).strip().lower()
        if result in yes_values:
            return True
        elif result in no_values:
            return False
        elif error_message is not None:
            print(error_message)

# reference list of legislators            
member_party = {
    "Tsang Yok-sing": "DAB",
    "Albert Ho":      "Democratic",
    "Lee Cheuk-yan":  "Labour",
    "James To":       "Democratic",
    "Chan Kam-lam":   "DAB",
    "Lau Wong-fat":   "Economic Synergy",
    "Emily Lau":      "Democratic"
}

members = list(member_party.keys())
parties = list(member_party.values())
# For each party, we keep a list of all parties except itself
#   (this is used to balance questions so each question
#   has a 50% chance of being correct)
other_parties = {party:[p for p in parties if p != party] for party in parties}

def do_question():
    # pick a member at random
    member = choice(members)
    party = member_party[member]
    test_party = party if random() < 0.5 else choice(other_parties[party])
    # question user
    prompt = "Does {} belong to the {} party? [Y/n] ".format(member, test_party)
    answer = get_yn(prompt)
    # score answer
    if answer:
        if party == test_party:
            print("You are right!")
            return True
        else:
            print("Sorry, {} is from the {} party.".format(member, party))
            return False
    else:
        if party == test_party:
            print("Sorry, {} actually is from the {} party!".format(member, party))
            return False
        else:
            print("You are right! {} is from the {} party.".format(member, party))
            return True

def main():
    print("Welcome to the Whose Party Is This quiz:")
    correct = sum(do_question() for _ in range(NUM_QUESTIONS))
    print("You got {}/5 correct!".format(correct))

if __name__ == "__main__":
    main()