Quiz project in python

How to build your own quiz in python

In python there are a variety of ways to code a quiz. You can use basic print statements and input functions or you can use for loops to make the same quiz more concise.

The provided code is a basic quiz program that interacts with users. Here are the elements that make up most of the code:

  • import getpass, sys: This imports the modules.

  • def question_with_answers(prompt): This defines a function that prints a question, collects user input as an answer, and stores the answer

  • questions = 3: This is the total number of questions in the quiz.

  • correct_answers = 0: This variable keeps track of the amout of correct answers.

  • question_list: This list contains the questions for the quiz.

  • ans_list: This list holds the correct answers corresponding to the questions.

  • The for loop iterates over each question:

    • rsp = question_with_answers(question_list[i]): This line asks the user a question and stores their response into a variable

    • if ans_list[i] == rsp:: This checks if the user’s response matches the correct answer.

      • If the answer is correct, correct_answers added by one, and a success message is displayed.

      • If the answer is incorrect, an print message is shown to help them know they got the question wrong

  • At the end code displays a completion message for the quiz and calculates the user’s percentage score.

import getpass, sys
#Defines a function that we can call it in future lines of code
def question_with_answers(prompt):
    print("The Question is:" + prompt)
    msg = input()
    return msg


# Variables to calculate percent at the end
questions = 3
correct_answers = 0
#List of questions
question_list = ["What Command is used to get other snippets of codes other people built into your file?", "What command is used to define a function?", "What Command is used to add a variable into a string?" ]
#list of answers correlated directly with the placement of questions
ans_list = ["import", "def", "string concatenation"]
#for loop where it helps make your code more concise. It itterates through lists to check answers
for i in range(0,3):
    rsp = question_with_answers(question_list[i])
    print(f"Your Answer is {rsp}")
    if ans_list[i] == rsp:
        print("Your Answer is Correct")
        correct_answers += 1
    else:
      print ("Your Answer is incorrect!")  


#Prints that you have completed the quiz
print("Congrats you have successfully completed the quiz!")
#calculates the percentage
percent = int(correct_answers)/int(questions) 
percent = percent * 100
#Prints your percentage with a message
print("The percentage of your test is: " + str(percent) + "%")





The Question is:What Command is used to get other snippets of codes other people built into your file?
Your Answer is import
Your Answer is Correct
The Question is:What command is used to define a function?
Your Answer is def
Your Answer is Correct
The Question is:What Command is used to add a variable into a string?
Your Answer is f
Your Answer is incorrect!
Congrats you have successfully completed the quiz!
The percentage of your test is: 66.66666666666666%