Python Quiz Post
Making a quiz using python.
import getpass, sys
def question_with_response(prompt):
print("Question: " + prompt)
msg = input()
return msg
questions = 3
correct = 0
print('Hello, ' + getpass.getuser() + " running " + sys.executable)
print("You will be asked " + str(questions) + " questions.")
question_with_response("Are you ready to take a test?")
rsp = question_with_response("What command is used to include other functions that were previously developed?")
if rsp == "import":
print(rsp + " is correct!")
correct += 1
else:
print(rsp + " is incorrect!")
rsp = question_with_response("What command is used to evaluate correct or incorrect response in this example?")
if rsp == "if":
print(rsp + " is correct!")
correct += 1
else:
print(rsp + " is incorrect!")
rsp = question_with_response("Each 'if' command contains an '_________' to determine a true or false condition?")
if rsp == "expression":
print(rsp + " is correct!")
correct += 1
else:
print(rsp + " is incorrect!")
print(getpass.getuser() + " you scored " + str(correct) +"/" + str(questions))
def Python_Quiz(prompt):
global word
#I made the variable "word" global so that I could call it outside of the function
print ("Question: " + prompt)
#Every question begins with "Question:"
word = input()
#The variable "word" is the input
return word
#returns the variable "word"
questions = 5
correct_answer = 0
#Made 2 new variables "questions" and "correct_answer"
print("Hello, you will be asked " + str(questions) + " short questions")
#Introduces the quiz
question_list = ["What is the answer to this math problem: What is f(4) if f(x) = 3x/x-3", "What is the most popular religion in the world?",
"What is the capital of the U.S?", "What is the molar mass of Carbon-12?", "How many moles of water are in a 3.0 gram sample?"]
answer_list = ["12", "Christianity", "Washington D.C.", "12 g", "0.17"]
#I made 2 lists for questions and answers
for i in range(5):
Python_Quiz(question_list[i])
if word == answer_list[i]:
print("Answer is correct")
correct_answer += 1
else:
print("Answer is incorrect")
#If the answer is correct to its respective question, then a correct answer response pops up. If it isn't correct, then an incorrect answer response pops up.
print("You scored " + str(correct_answer) + "/" + str(questions))
#SHows how many correct answers the user got
Explanation
Inside the function: I defined the function Python_Quiz, I made a variable "word" and I also made it so that every question started with the format "Question:". I defined the variable "word" so that it was equal to the input of the user. I also made the variable global so that it could be called outside of the function.
Outside the function: I made 2 new variables "questions", and "correct_answer" which is later used to say the amount of question and correct answers. I then introduce the quiz by saying that the user will be asked 5 questions. For extra credit, I used lists and iterations to make a question list and an answer list. Then I used index (i) to make it so that if the user's answer was equal to the corresponding answer_list answer, then a correct answer response was given. If the answer that the user gave did not match the corresponding answer_list answer, then an incorrct answer response was given. I then also told the user how many correct answers they got out of the total amount of questions.