Skip to the content.

My Python Quiz • 7 min read

Description

Quiz I created based on python vocab

Welcome to Grayson Guyot’s notebook on creating a quiz !!

What I am going to use to create my quiz:

print(), import, def(), if, else, while, for, dictionary

First, let’s start by importing the getpass and sys libraries.

There are two functions that we are going to use: getpass.getuser() and sys.executable.
getpass.getuser() prints the user’s name.
sys.executable prints the path to the Python interpreter.
First, let’s start by importing the sys and getpass libraries.
import sys
import getpass
Next, let’s create a dictionary that contains our questions and answers. The keys are our questions and the values are our answers. Let’s also assign the number of questions to the length of the dictionary, and number of correct answers to another variable.
# questions and answers dictionary
qsandas_dict = {
    "What command is used to include other functions that were previously developed?" : "import",
    "What command is used to evaluate correct or incorrect response in this example?" : "if",
    "Each 'if' command contains an '_________' to determine a true or false condition?" : "expression",
    "What is the word used to describe somthing that cannot change?" : "static"
}

# number of questions, number of correct answers (this will be incremented later)
questions = len(qsandas_dict) # length of the dictionary
correct = 0
- Greet the user and print their username and the path to the Python interpreter.
- Prompt the user and ask if they are ready to take the test.
print(f"Hello, {getpass.getuser()} running {sys.executable}")
print(f"You will be asked {questions} questions.")
readyornot = input("Are you ready to take a test? ")
Hello, graysonguyot running /opt/homebrew/opt/python@3.11/bin/python3.11
You will be asked 4 questions.
If they are ready to take the test, start iterating through the dictionary, asking questions and prompting for answers. If they type the correct answer, say that they are correct and increment the variable containing the number of correct answers. If they’re wrong, say that they’re incorrect and print out the correct answer. Print out the final score as a percentage.
If they’re not ready to take the test, tell them to come back another time.
If they give an invalid response, tell them that their response is invalid.
# if they are ready
if readyornot == "yes" or readyornot == "Yes":
    print("Awesome! Let's begin.")
    
    # for each question in our dictionary, ask the question
    for question in qsandas_dict:
        answer = input(question)
        print(f"You answered {answer}.")
        
        # if they are correct, increment correct variable
        if answer == qsandas_dict[question]:
            print("You are correct!")
            correct += 1
        
        # if they are wrong, print out correct answer
        else:
            print(f"You are incorrect! The answer is {qsandas_dict[question]}.")
            
    score = (correct/questions) * 100
    #print out results
    print(f"{getpass.getuser()} you scored {correct} / {questions}, which is a {score:.2f}%.")
    
elif readyornot == "no" or readyornot == "No":
    print("No problem! Come back when you're ready!")
else:
    print("Invalid response. Please try again.")