Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7603095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:34:55+00:00 2026-05-30T23:34:55+00:00

I am learning Python and am making a Q&A script. I made one function

  • 0

I am learning Python and am making a Q&A script. I made one function for the questions. That went rather well. Now I am wanting an average function. I want to avoid using globals if at all possible. I know that my variables reset at the top… can someone please give me some pointers? I know C/PHP/BASIC and want to grasp this langauge. Below is my question function.

    def q(question, a, b, c, c_answer):
        total,tally=0,0
        print "",question
        print "   a.",str(a)
        print "   b.",str(b)
        print "   c.",str(c)
        u_answer = raw_input()
        if c_answer == "a" and u_answer == "a":
            print "Correct, the answer is A!"
            tally+=1
        elif c_answer == "b" and u_answer == "b":
            print "Correct, the answer is B!"
            tally+=1
        elif c_answer == "c" and u_answer == "c":
            print "Correct, the answer is C!"
            tally+=1
        else:
           print "I am sorry, but the correct answer is",c_answer
        print "\..n"
        total+=1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:34:56+00:00Added an answer on May 30, 2026 at 11:34 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Python right now and I want to write some script that will
I am learning Python and am reading through an example script that includes some
I am learning Python for a class now, and we just covered tuples as
I've been learning python for a while now with some success. I even managed
I recently started learning Python and I was rather surprised to find a 1000
I'm learning Python now because of the Django framework. I have been a Perl
I was learning python using the tutorial that comes with the standard python installation.
I'm learning Python and would like to start a small project. It seems that
I just started learning Python... I am writing a simple program that will take
I'm learning python and trying to write a code to sync two directories: one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.