Since you're just learning Python, I reformatted your code to be more pythonic.
def q(question, a, b, c, c_answer):
total, tally = 0, 0
print "", question
print " a. %s" % a
print " b. %s" % b
print " c. %s" % c
u_answer = raw_input("your answer? ").strip()
if c_answer == u_answer:
print "Correct, the answer is %s!" % u_answer
tally += 1
else:
print "I am sorry, but the correct answer is %s" % c_answer
print "\n"
total += 1
To actually answer your question:
There a couple ways to keep track of the total number of questions and correct answers without using global-level variables (actually module-level, but that's a different topic ;).
One is to pass in the current totals, have q recalculate based on the current question, and then pass them back out:
def q(question, a, b, c, c_answer, total, tally):
print "", question
print " a. %s" % a
print " b. %s" % b
print " c. %s" % c
u_answer = raw_input("your answer? ").strip()
if c_answer == u_answer:
print "Correct, the answer is %s!" % u_answer
tally += 1
else:
print "I am sorry, but the correct answer is %s" % c_answer
print "\n"
total += 1
return total, tally
Then in your main program you can say:
question_pool = (
('What is 2 + 2?', 2, 3, 4, 'c'),
('What is blue mixed with yellow?', 'green', 'orange', 'pink', 'a'),
('How far does light travel in one nanosecond?', '10 mm', '20 cm', '30 m', 'b'),
)
total, tally = 0, 0
for packet in question_pool:
question, a, b, c, answer = packet
total, tally = q(question, a, b, c, answer, total, tally)
print "you answered %d correctly, for a score of %2.0f%%" % (tally, 100.0 * tally / total)
However, it would be better for q to just deal with questions, and not worry about keeping track of how many questions have been answered and how many questions have been asked.
So instead of accepting total and tally, recalculating, and returning total and tally, q will now just return 0 if the answer was wrong, 1 if it was correct:
def q(question, a, b, c, c_answer):
print "", question
print " a. %s" % a
print " b. %s" % b
print " c. %s" % c
u_answer = raw_input("your answer? ").strip()
if c_answer == u_answer:
print "Correct, the answer is %s!\n" % u_answer
return 1
print "I am sorry, but the correct answer is %s" % c_answer
return 0
and the rest of the code looks like:
question_pool = (
('What is 2 + 2?', 2, 3, 4, 'c'),
('What is blue mixed with yellow?', 'green', 'orange', 'pink', 'a'),
('How far does light travel in one nanosecond?', '10 mm', '20 cm', '30 m', 'b'),
)
total, tally = 0, 0
for packet in question_pool:
question, a, b, c, answer = packet
tally += q(question, a, b, c, answer)
total += 1
print "you answered %d correctly, for a score of %.0f%%" % (tally, 100.0 * tally / total)