I am preparing for my a+ exams, and am turning all of my notes into a test. I plan on doing this in python, of course. But, I am a little lost at what would be best. Should I create each question as a specific function, calling the next question function when i am done with that question? Or should I create one function to handle the questions, and functions for everything else? My plan for the format of the test is such:

present the question.
prompt the user to press enter when ready for answer
user presses enter
clear screen and begin again

this will go until there are no more questions left to ask, in which case the screen will clear and ask if you want to study again or quit.

thank you for your help.

If it is a something like a mutliple choice quiz, you can organize all the questions/answers into a list of tuples. Each tuple might have this order:
(question, answer1, answer2, answer3, answer4, answer_code)
Each of these tuples comes from a data file were each question looks like this:
question
answer1
answer2
answer3
answer4
answer_code
marker
next question
...

This arrangement allows you to random shuffle the tuples in the list, and pop properly answered question tuples out of the list. If you want to, you can encode the data file to keep folks from cheating openly.

A good activity instead of learning:)

I suppose you have some file with the questions...
So it can be loaded into a structure
questions=[question1, question2,....]

questions=["1","2","3"] # as test
qc=0 # actual question index
print("press enter for the next question q and enter to quit")
while True:
    print(questions[qc])
    key=raw_input()
    if key=="q": break
    print("\n"*45) # prints a lot of newline
    #next question
    qc+=1
    if qc==len(questions): qc=0 # return to the beginning

If this get boring, you can randomize the questions.

import random
#the same code
    #next question
    qc=random.randint(0,len(questions)-1)

I hope this helps.
Anyway, what is an a+ exam? I am not from the US.

Member Avatar for leegeorg07

i'm not sure how you would do it but i would recommend a class which you can then define for each question

Thanks for all of the advice guys, I actually put a a little of what each of you said into one method. An A+ exam is what you take to show that you have a good knowledge of how to troubleshoot computers, from a hardware and software perspective. If you pass your A+ exams, you become A+ certified. I am pretty sure you need to pass with a 95% or higher, which makes me nervous as if I fail, I have to pay to take the test again, which is like 186 USD each time.

Anyways, here is the start of my code, it just has 14 basic questions, but it gives an idea of where I am going with it. Maybe this code could help others who may be preparing for their A+ exams.

import sys, os

global score
score = 0
def clear():
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print
    print

    
    
q1 = """What is the Binary number system"""
a1 = """The Binary number system is a number system that uses only the numbers 1 and 0, where 1 represents "on",
  and 0 represents "off"."""
q2 = """Explain how the CPU uses the Binary number system."""
a2 = """The CPU takes input from input devices, such as the mouse or keyboard, and translates the data and instructions
    the input device sends to it into Binary form. The CPU then sends the Binary data and instructions to an output device,
    such as the monitor or the printer. In other words, if you type the word "Tom" into the cpu, the letters "t" "o" and "m"
    are converted into their binary form, consisting of the numbers 1 and 0. When you decide to print the file, the CPU sends
    the binary to the printer, which then converts the 1's and 0's back into the word "Tom"."""
q3 = """What is the most important component inside a PC?"""
a3 = """The most important component inside a PC is the CPU, or Central Processing Unit. It is also called the processor and
    microprocessor."""
q4 = """What is the CPU's job, in general?"""
a4 = """The general job of the CPU is to recieve data from input devices (such as a keyboard or a mouse), and then to write the
    output to output devices, such a a printer or a mouse."""
q5 = """What are the three (3) things every hardware, input/output and storage device need in order to operate?"""
a5 = """In order for every hardware, input/output and storage device to operate, the device will need:
                1) A method for the CPU to communicate with the device
                2) Software to instruct and control the device
                3) Electricity to power the device
                """
q6 = """What is a port?"""
a6 = """A port is a connection attached to the case of the computer. Common port types are USB (Universal Serial Bus), Parallel, and Serial ports."""
q7 = """What is an ENHANCED KEYBOARD?"""
a7 = """An ENHANCED KEYBOARD is a modern keyboard which holds 104 keys."""
q8 = """What is an ERGONOMIC KEYBOARD?"""
a8 = """An ERGONOMIC KEYBOARD is a keyboard which is curved to be more comfortable on the hands and wrist."""
q9 = """What are the FOUR MOST POPULAR CONNECTORS for printers and monitors?"""
a9 = """The FOUR MOST POPULAR CONNECTORS for printers and monitors are:
            1) 15-pin analog video connector
            2) 25-pin parallel connector
            3) DVI connector
            4) USB
            """
q10 = """What components do most computers contain inside the case?"""
a10 = """Most computers contain the following components inside the case...

                1) Motherboard containing the CPU, memory, and other components
                2) A Floppy drive, hard drive, and CD drive used for secondary storage (permanent storage)
                3) A power supply with power cords supplying electricity to all devices inside the case.
                4) Circuit boards used by the CPU to communicate with devices inside and outside the case.
                5) Cables connecting devices to circuit board and the mother board.
                """
q11 = """What does a parallel port do?"""
a11 = """A parallel port transmits data in parallel and is most often used by a printer."""
q12 = """What is an S/PDIF port?"""
a12 = """An S/PDIF port, which stands for Sony/Philips Digital Interface, connects to an external home theater audio system,
    providing digital output and the best signal quality."""
q13 = """What is a firewire port?"""
a13 = """A firewire port, also called a 1394 port, is used for high speed multimedia devices such as digital camcorders."""
q14 = """What are the MAJOR COMPONENTS FOUND ON ALL MOTHER BOARDS?"""
a14 = """The MAJOR COMPONENTS FOUND ON ALL MOTHERBOARDS ARE:

    1) CPU, the computers most important chip
    2) Chipset that supports the processor by controlling many motherboard activities
    3) RAM (Random Access Memory) used to hold data and instructions as they are processed.
    4) Cache memory to speed up memory access (optional, depends on the type of processor)
    5) Wires, (called traces), onthe motherboard used for communication
    6) Expansion slots to connect expansion cards to the mother board
    7) The system clock that keeps communication in sink
    8) Connections for data cables to devices inside the case
    9) Ports for devices outside the case
    10) Power supply connections to provide electricity to the motherboard and expansion cards
    11) Flash ROM, a memory chip used to permanently store instructions that control basic hardware functions
    12) CMOS RAM and CMOS setup chip that holds configuration data
    """

def Main():
    print '''

    Welcome to the A+ exam simulators. Type the number of the available section and press enter to enter the quiz for that section.

    1) Section 1

    * More sections coming shortly
    '''
    print
    x = raw_input("Type the number of your selection and press enter: ")
    if x == "1":
        sec_1()
    else:
        print "That is not a registered selection."
        print
        clear()
        Main()

def sec_1():
    ques1()
        
        
    
    
def ques1():
    clear()
    print q1
    print
    x = raw_input("Press enter for answer: ")
    if x == "":
        clear()
        print a1
        x = raw_input("Press enter for next question.")
        if x == "":
            ques2()
        else:
            ques2()
    else:
        clear()
        print a1
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques2()
        else:
            ques2()
                   
def ques2():
    
    clear()
    print q2
    print
    x = raw_input("Press enter for answer: ")
    if x == "":
        clear()
        print a2
        x = raw_input("Press enter for next question.")
        if x == "":
            ques3()
        else:
            ques3()
    else:
        clear()
        print a1
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques3()
        else:
            ques3()

def ques3():
    
    clear()
    print q3
    print
    x = raw_input("Press enter for answer: ")
    if x == "":
        clear()
        print a3
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques4()
        else:
            ques4()
    else:
        clear()
        print a3
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques4()
        else:
            ques4()

def ques4():
    
    clear()
    print q4
    print
    x = raw_input("Press enter for answer: ")
    if x == "":
        clear()
        print a4
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques5()
        else:
            ques5()
    else:
        clear()
        print a4
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques5()
        else:
            ques5()

def ques5():
    
    clear()
    print q5
    print
    x = raw_input("Press enter for answer: ")
    if x == "":
        clear()
        print a5
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques6()
        else:
            ques6()
    else:
        clear()
        print a5
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques6()
        else:
            ques6()

def ques6():
    
    clear()
    print q6
    print
    x = raw_input("Press enter for answer: ")
    if x == "":
        clear()
        print a6
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques7()
        else:
            ques7()
    else:
        clear()
        print a6
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques7()
        else:
            ques7()

def ques7():
    
    clear()
    print q7
    print
    x = raw_input("Press enter for answer: ")
    if x == "":
        clear()
        print a7
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques8()
        else:
            ques8()
    else:
        clear()
        print a7
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques8()
        else:
            ques8()

def ques8():
    clear()
    print q8
    print
    x = raw_input("Press enter for answer: ")
    if x == "":
        clear()
        print a8
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques9()
        else:
            ques9()
    else:
        clear()
        print a8
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques9()
        else:
            ques9()

def ques9():
    
    clear()
    print q1
    print
    x = raw_input("Press enter for answer: ")
    if x == "":
        clear()
        print a1
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques10()
        else:
            ques10()
    else:
        clear()
        print a1
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques10()
        else:
            ques10()

def ques10():
    clear()
    print q10
    print
    x = raw_input("Press enter for answer: ")
    if x == "":
        clear()
        print a10
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques11()
        else:
            ques11()
    else:
        clear()
        print a10
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques11()
        else:
            ques11()

def ques11():
    clear()
    print q11
    print
    x = raw_input("Press enter for answer: ")
    if x == "":
        clear()
        print a11
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques12()
        else:
            ques12()
    else:
        clear()
        print a11
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques12()
        else:
            ques12()

def ques12():
    clear()
    print q12
    print
    x = raw_input("Press enter for answer: ")
    if x == "":
        clear()
        print a12
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques13()
        else:
            ques13()
    else:
        clear()
        print a12
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques13()
        else:
            ques13()

def ques13():
    clear()
    print q13
    print
    x = raw_input("Press enter for answer: ")
    if x == "":
        clear()
        print a13
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques14()
        else:
            ques14()
    else:
        clear()
        print a13
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            ques14()
        else:
            ques14()

def ques14():
    clear()
    print q14
    print
    x = raw_input("Press enter for answer: ")
    if x == "":
        clear()
        print a14
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            sec_end()
        else:
            sec_end()
    else:
        clear()
        print a14
        print
        x = raw_input("Press enter for next question.")
        if x == "":
            sec_end()
        else:
            sec_end()

def sec_end():
    clear()
    print """This completes section 1. The other sections have not yet been written. Study these sections until you get them all right.

    press a to restart, or simply quit.
    """
    print
    x = raw_input("Press a to restart... ")
    if x == 'a':
        ques1()
    else:
        pass
        
    
Main()

There will be miultiple sections, and I would like to learn how to make a multi file source code, so I could have the questions in one file. How would I work this, if the questions are in a file called questions.py, in the exact same path as the main file?

Instead of all those print statements at the top, you can put

print "\n" * (blank lines you desire - 1)

Yes. The clear function can be just print("\n"*55).

You can put the questions and answers in a file like:

Q
What is the Binary number system?
A
The Binary number system is a number system that uses only the numbers 1 and 0, where 1 represents "on",
  and 0 represents "off".
Q
Explain how the CPU uses the Binary number system.
A
The CPU takes input from input devices, such as the mouse or keyboard, and translates the data and instructions
    the input device sends to it into Binary form. The CPU then sends the Binary data and instructions to an output device,
    such as the monitor or the printer. In other words, if you type the word "Tom" into the cpu, the letters "t" "o" and "m"
    are converted into their binary form, consisting of the numbers 1 and 0. When you decide to print the file, the CPU sends
    the binary to the printer, which then converts the 1's and 0's back into the word "Tom".

etc...
So you dont have to type the apostrophes and the variable names.
Just take care, that before a question appears a Q in a single line, with the answer similar.
Let's name that sec1.txt.

We can load this file into a list containing question and answer pairs, some way:

def load_section(fname):
    section=[] #question and answer pairs
    f=open(fname)
    mode="q"
    q=""
    a=""
    for line in f:
        if line.strip()=='Q':
            if q and a: section.append((q,a))
            q=""
            a=""
            mode="q"
        elif line.strip()=='A':
            mode="a"
        else:
            if mode=="q":
                q+=line
            else:
                a+=line
    if q and a: section.append((q,a))    
    return section

So the code for sec_1 can be:

def sec_1():
    section=load_section("sec1.txt")
    clear()
    for q,a in section:
        print(q)
        raw_input("Press enter for answer")
        print(a)
        raw_input("Press enter for next question: ")  
        clear()
    clear()   
    print """This completes section 1. The other sections have not yet been written. Study these sections until you get them all right.

    press a to restart, or simply quit.
    """
    print
    x = raw_input("Press a to restart... ")
    if x == 'a':
        sec_1()

And the gues* functions go away....

EDIT: Woops, accidentaly just posted basically the same as the above. Sorry

No problem. I will definitely take a look at doing things that way, anything to shorted up my code, and make it more effective. The main goal is to quickly create a study partner to help me prepare for the exams. Thanks a lot, everyone, you have helped me out enormously!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.