Reputation: 686
So I have a dictionary with legislators and the parties they belong to. Five questions are outputted with random names and parties and the input is either Y or N. I need to now figure out how to check if its true or not but I am stumped. The code:
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")
Just need to get any advice on where to start on this. Thanks a lot!
Upvotes: 1
Views: 1272
Reputation: 337
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 = raw_input("Does "+name+" belong to "+party+ " Y/N?")
if ans == 'Y':
if legislators[name] == party:
print 'correct'
else:
print 'error'
elif ans == 'N':
if legislators[name] == party:
print 'error'
else:
print 'correct'
Upvotes: 2
Reputation: 56714
There is another problem with your program:
Picking a random member and a random party gives a 34/49 or approximately 70% chance of the party being wrong, so always answering 'n' will give an average score of 3.47 / 5 correct.
We can fix this like so:
# 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])
I have also:
get_yn()
which accepts a variety of yes and no values and returns True
for yes and False
for norandom.choice
to pick a member instead of indexing with random.randrange
do_question()
function which returns True
for a correct answer and False
for a wrong oneThe result:
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()
Upvotes: 1
Reputation: 21934
Adding a code example. You code is hardcoded to upper case Y/N.
Just before the input
compute the validity of your random combination.
if legislators[name] == party:
valid = "Y"
else:
valid = "N"
Now after the input
you need to do this:
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."
Upvotes: 0
Reputation: 600059
Since you have the originals stored in a dict, you can just check if legislators[name] == party
.
Upvotes: 0