Hi! I'm here to show you my Python script. But first for the hacks... I got an image and a link. The link is for my vocab table, and the image is below.

Ok, enough of that. I'll show you the code in its full beauty first, and if you need help understanding how any part of it works, I'll explain as thoroughly as I can.

import getpass, sys 

#defines question and answer: a prompt variable goes in the parenthesis. It essentially spits out user input.
def question_and_answer(prompt): 
    print("Question: " + prompt)
    msg = input()
    print("Answer: " + msg)

#defines question_with_response: collects the questions from a list with prompt, prints it, collects input, and stores it.
def question_with_response(prompt): 
    print("Question: " + prompt)
    msg = input()
    return msg

#variables for number of questions and number of correct answers
questions = 10
correct = 0

#initial prompt
print('Hi ' + getpass.getuser())
print("You will be asked " + str(questions) + " questions.")
question_and_answer("Are you ready to take a test?")

#list of questions and answers. Also converts answer list into iteration so next command works.
Question_list = ["Is your answer input or output?", "What command is used to include other functions that are developed?", "What is the collection of these functions called?", "What command was used to make the function that gave you this question?", "what is Answer_list in iter(Answer_list)?", "What command in this example is used to evaluate a response?","Each 'if' command contains an '_________' to determine a true or false condition?", "What type of text is the variable called 'correct' in this script?", "What is it called to print a string (as output) to terminal?", "What is a group of commands defined under a name?"]
Answer_list = ["input", "import", "library", "def", "parameter", "if", "expression", "dynamic text", "string concatenation", "function"]
Answer_iter = iter(Answer_list)

#cycles through this set of command for each set of question and answer. Also accepts correct answers and rejects bad answers.
for i in Question_list:
    rsp = question_with_response(str(i))
    if rsp == str(next(Answer_iter)):
        print(rsp + " is correct!")
        correct += 1
    else:
        print(rsp + " is incorrect!")
            
#Calculates percent correct answers.
percent = (correct/questions) * 100 
    
#Prints accuracy of answers to terminal
print("You scored " + str(percent) + '%!') 
Hi leonardw48247
You will be asked 10 questions.
Question: Are you ready to take a test?
Answer: 
Question: Is your answer input or output?
input is correct!
Question: What command is used to include other functions that are developed?
import is correct!
Question: What is the collection of these functions called?
library is correct!
Question: What command was used to make the function that gave you this question?
def is correct!
Question: what is Answer_list in iter(Answer_list)?
parameter is correct!
Question: What command in this example is used to evaluate a response?
if is correct!
Question: Each 'if' command contains an '_________' to determine a true or false condition?
expression is correct!
Question: What type of text is the variable called 'correct' in this script?
expression is incorrect!
Question: What is it called to print a string (as output) to terminal?
string concatenation is correct!
Question: What is a group of commands defined under a name?
function is correct!
You scored 90.0%!

Explanation Portion

Alright... where to start... oh, the import commands would be a good start!

import getpass, sys

This imports the functions getpass and sys into the script, making them executable. Where did those functions come from? They came from the libraries, which is essentially a collection of functions that comes with your python installation. Pretty neat!. Next, we got our own function.

def question_and_answer(prompt):
    print("Question: " + prompt)
    msg = input() 
    print("Answer: " + msg)
  • On the first line, we got the def command, which defines our function. The function's name is question_and_answer (fairly intuitive since it asks you a question and requests for an answer), and there is a prompt in the parenthesis. This prompt is a variable, and whenever this function is called (this should look like question_and_answer("insert text here")), the variable prompt is set to whatever is within the parenthesis. The colon is there for syntax purposes and should be kept.
  • On the second line, the print() command is used. Within the parenthesis is "Question: " to ensure that a question prompt is printed when the file is executed, along with + prompt. The + concatenates/prints whatever text/string is stored in the prompt variable. The output to the terminal is Question: (prompt text here)
  • On the third line, a variable named msg is set equal to the result of the command input(). As the name implies, the function will collect user input and set it equal to a variable. This also means the script will pause when user input is being collected.
  • On the fourth line, the print() command is used again, this time with the string Answer: to proceed the text of the variable msg, which was set equal to the input on the third line.

So to sum it up, this function collects an input string from a user and re-prints it to the terminal. Let's move on to the other function.

def question_with_response(prompt): 
    print("Question: " + prompt) 
    msg = input()
    return msg

If you paid attention, the first three lines of this function question_with_response were very similar to the first three lines of the question_with_answer function. The last line is what we want to look at. The return command ends the execution of the function and sends the contents of msg to the main script in execution. The contents of msg can therefore be used outside the function.

questions = 10
correct = 0

These are more variables. The variable questions lets Python know how many questions there are, because knowledge of how many questions there are is necessary for score calculation. correct, the number of correct answers, is 0 since no questions have been answered yet.

print('Hi ' + getpass.getuser())
print("You will be asked " + str(questions) + " questions.") 
question_and_answer("Are you ready to take a test?")
  • The first line prints to the prompt, and this is the first line of code executed. getpass.getuser() is linked to that function we imported at the beginning, and it essentially is a placeholder for the username of the person running the script. Might as well say hi and call the test-taker by his/her name!
  • The second line prints about being asked questions. str(questions) must be used to print the number of questions since questions is set equal to a number (it must be set equal to a string to print, and the str() command converts it into such).
  • The third line is our question_and_answer function from before! If you remember what it does, it prints out the prompt in the parenthesis, collects a user input, and prints that input into the prompt.

Now for the next bit of code!

Question_list = ["Is your answer input or output?", "What command is used to include other functions that are developed?", "What is the collection of these functions called?", "What command was used to make the function that gave you this question?", "what is Answer_list in iter(Answer_list)?", "What command in this example is used to evaluate a response?","Each 'if' command contains an '_________' to determine a true or false condition?", "What type of text is the variable called 'correct' in this script?", "What is it called to print a string (as output) to terminal?", "What is a group of commands defined under a name?"]
Answer_list = ["input", "import", "library", "def", "parameter", "if", "expression", "dynamic text", "string concatenation", "function"]
Answer_iter = iter(Answer_list)

The first two lines are relatively self-explanatory (though lengthy). I made a list called Question_list that has a list of all the questions of the quiz, and Answer_list has all the answers. The first items on the lists will be the ones to appear on the quiz first, but more on that later. The third line is less self-explanatory. I essentially converted my answer list into an iteration. Why would I do that? Well really it's because there's a command in the next bit of code that doesn't work if I don't convert the list into an iteration. That's just the way it is. Now it's time for the exciting part.

for i in Question_list:
    rsp = question_with_response(str(i))
    if rsp == str(next(Answer_iter)):
        print(rsp + " is correct!")
        correct += 1
    else:
        print(rsp + " is incorrect!")

I know this is a lot to take in. Bear with me for a little while.

  • The first line is a for loop. The i is a variable used to count how many items are in the related list, Question_list, and how many times the commands within the for loop are executed. You could use any variable, though i is the most common (probably since the word "iteration" starts with an i, though I could be wrong). All commands connected to the for loop (which is this entire chunk of code) is executed once for every question in Question_List. Why would I want to do that? Let's move to Line 2.
  • In the second line, we set a rsp variable. We call the question_with_response function, which if you remember prints a question prompt and collects input, sending it to the main script. This input will be set equal to the rsp variable. Notice what is inside the function's parenthesis: str(i). Remember i is the variable from the for loop which is going over each question in Question_list. So every time this is executed, the prompt will be the next question in the list.
  • In the third line, there is an if statement. If the condition of the if statement is satisfied, any connected code is executed. In this case, the condition is that the input variable rsp is equal to... let's deconstruct that code.
    • str(), as mentioned before, turns a number/value into a string. In my tests, the printing of the answer was being picky, so I used this command even though there's no numbers involved.
    • next(Answer_iter) is the way that the next answer on the list will be used every time the command is executed. next() moves on to the next item in the iteration whenever executed. For some reason, next() doesn't accept lists, which is why we made Answer_iter.
  • So back to what I was saying... the condition is that the input variable rsp be equivalent to the correct answer of the question in the prompt. If this condition is satisfied, appropriate code is executed for a correct answer:
  • In line 4, we print their input (still stored in rsp) and also that it's correct.
  • In line 5, we increase the correct variable by one since a correct answer was given.
  • Line 6 is else. If the condition of the if statement is not fulfilled, any code connected to the else statement is executed instead. That would mean getting a question wrong.
  • So line 7 prints that the input was incorrect.

These 7 lines execute 10 times for the 10 questions. Doing this makes the code more concise than typing out the same commands 10 times in a script. Now, the rest of the script should be a breeze.

percent = (correct/questions) * 100

This line, as implies, calculates the percentage of questions correctly answered. It takes the number of questions correctly answered and divides by total number of questions (these are all variables by the way, which are set equal to values/numbers), then multiplies by 100. This number is set equal to the percent variable.

print("You scored " + str(percent) + '%!')

This line prints the percent score. Keep in mind that since the variable percent has a numeric value that the str() command must be used to convert it to a string.

That's the end of the script! I hope you had a good time learning about how this works, and I hope you will use some of these techniques in your future work.