Reputation: 1
I'm reading Zed Shaw's _Learn Python The Hard Way, _ and I've progressed to exercise 27, which essentially is having to memorize boolean logic "tables". So I decided to make a quick test in python for this, but it doesn't behave correctly no matter what I do. If I put in every right answer, it gives a score of approximately 40%, when it should give a 100. The script is below. The correct answers are on the website.
import time
Answers = ["True", "False", "True", "True", "True", "False", "False", "True", "False", "False", "False", "False", "False", "True", "True", "False", "True", "True", "True", "False", "True", "False", "False", "True", "False", "True"]
q1 = raw_input("not False = ")
q2 = raw_input("not True = ")
q3 = raw_input("True or False = ")
q4 = raw_input("True or True = ")
q5 = raw_input("False or True = ");
q6 = raw_input("False or False = ")
q7 = raw_input("True and False = ")
q8 = raw_input("True and True = ")
q9 = raw_input("False and True = ")
q10 = raw_input("False and False = ");
q11 = raw_input("not (True or False) = ")
q12 = raw_input("not (True or True) = ")
q13 = raw_input("not (False or True) = ")
q14 = raw_input("not (False or False) = ")
q15 = raw_input("not (True and False) = ");
q16 = raw_input("not (True and True) = ")
q17 = raw_input("not (False and True) = ")
q18 = raw_input("not (False and False) = ")
q19 = raw_input("1 != 0 = ")
q20 = raw_input("1 != 1 = ");
q21 = raw_input("0 != 1 = ")
q22 = raw_input("0 != 0 = ")
q23 = raw_input("1 == 0 = ")
q24 = raw_input("1 == 1 = ")
q25 = raw_input("0 == 1 = ");
q26 = raw_input("0 == 0 = ");
cout = 0
if q1 == Answers[1]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q2 == Answers[2]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q3 == Answers[3]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q4 == Answers[4]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q5 == Answers[5]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q6 == Answers[6]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q7 == Answers[7]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q8 == Answers[8]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q9 == Answers[9]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q10 == Answers[10]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q11 == Answers[11]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q12 == Answers[12]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q13 == Answers[13]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q14 == Answers[14]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q15 == Answers[15]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q16 == Answers[16]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q17 == Answers[17]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q18 == Answers[18]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q19 == Answers[19]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q20 == Answers[20]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q21 == Answers[21]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q22 == Answers[22]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q23 == Answers[23]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q24 == Answers[24]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q25 == Answers[25]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
if q26 == "True":
cout = cout + 3.84615385
else:
cout = cout + 0.0
#
print "Calculating results..."
time.sleep(2)
print """
=============================
Your Score Was:
%s percent
=============================
""" % str(cout)
#
Upvotes: 0
Views: 68
Reputation: 9756
Instead of dealing with indices and hardcoded values you could create a list of question- and -answer pairs. Then loop through the questions using a for loop. This will allow you to add or remove questions without having to change anything else in the program!
import time
# This list contains tuples where the first element is the question and the second the answer.
questions = [
('not False = ', 'True'),
('not True = ', 'False'),
('True or False = ', 'True'),
('True or True = ', 'True'),
('False or True = ', 'True'),
('False or False = ', 'False'),
('True and False = ', 'False'),
('True and True = ', 'True'),
('False and True = ', 'False'),
('False and False = ', 'False'),
('not (True or False) = ', 'False'),
('not (True or True) = ', 'False'),
('not (False or True) = ', 'False'),
('not (False or False) = ', 'True'),
('not (True and False) = ', 'True'),
('not (True and True) = ', 'False'),
('not (False and True) = ', 'True'),
('not (False and False) = ', 'True'),
('1 != 0 = ', 'True'),
('1 != 1 = ', 'False'),
('0 != 1 = ', 'True'),
('0 != 0 = ', 'False'),
('1 == 0 = ', 'False'),
('1 == 1 = ', 'True'),
('0 == 1 = ', 'False'),
('0 == 0 = ', 'True')
]
score = 0
number_of_questions = len(questions)
for question, answer in questions:
if raw_input(question) == answer:
score += 100.0 / number_of_questions
print "Calculating results..."
time.sleep(2)
print """
=============================
Your Score Was:
%s percent
=============================
""" % str(score)
Upvotes: 1
Reputation: 127
EDIT: Someone beat me to it! The Answers, is an array. The first element in an array starts with 0 not 1. So you should start from Answers[0] in your if clauses. I may write the code this way:
import time
Answers = ["True", "False", "True", "True", "True", "False", "False","True","False", "False", "False", "False", "False", "True", "True","False", "True", "True", "True", "False", "True", "False", "False","True", "False", "True"]
q1 = raw_input("not False = "); q2 = raw_input("not True = ");
q3 = raw_input("True or False = "); q4 = raw_input("True or True = ");
q5 = raw_input("False or True = ");
q6 = raw_input("False or False = ");
q7 = raw_input("True and False = "); q8 = raw_input("True and True = ");
q9 = raw_input("False and True = ");
q10 = raw_input("False and False = ");
q11 = raw_input("not (True or False) = ");
q12 = raw_input("not (True or True) = ");
q13 = raw_input("not (False or True) = "); q14 = raw_input("not (False or False) = ");
q15 = raw_input("not (True and False) = ");
q16 = raw_input("not (True and True) = "); q17 = raw_input("not (False and True) = ");
q18 = raw_input("not (False and False) = ");
q19 = raw_input("1 != 0 = "); q20 = raw_input("1 != 1 = ");
q21 = raw_input("0 != 1 = "); q22 = raw_input("0 != 0 = ");
q23 = raw_input("1 == 0 = "); q24 = raw_input("1 == 1 = ");
q25 = raw_input("0 == 1 = ");
q26 = raw_input("0 == 0 = ");
Questions =[q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,q13,q14,q15,q16,q17,q18,q19,q20,q21,q22,q23,q24,q25,q26]
count = sum([3.8461 for i in range(26) if Questions[i] == Answers[i]])
print "Calculating results..."
time.sleep(2)
print("""
=============================
Your Score Was:
%s percent
=============================""" %str(cout))
Upvotes: 0
Reputation: 9753
It's because list indices start at 0, not 1. So your inputs are shifted by one relatively to the Answers
.
You first if
block should be:
if q1 == Answers[0]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
and the last one:
if q26 == Answers[25]:
cout = cout + 3.84615385
else:
cout = cout + 0.0
Upvotes: 2