Python quiz
import getpass, sys
print('Hello, ' + getpass.getuser() + " running " + sys.executable)
print("You will be asked three questions.")
# quiz render function
def quiz():
guesses = []
correct_guesses = 0
question_num = 1
for key in questions:
print(" ")
print(key)
for i in options[question_num-1]:
print(i)
guess = input()
guess = guess.upper()
guesses.append(guess)
correct_guesses += check_answer(questions.get(key), guess)
question_num += 1
display_score(correct_guesses, guesses)
# checks if the answer is correct or not
def check_answer(answer, guess):
if answer == guess:
print("CORRECT!")
return 1
else:
print("WRONG!")
return 0
# takes the number of correct guesses and divides it by the questions to display score
def display_score(correct_guesses, guesses):
print(" ")
print("RESULTS")
print(" ")
score = int((correct_guesses/len(questions))*100)
print("Your score is: "+str(score)+"%")
# questions formatted into a dictionary for ease of access
questions = {
"What command is used to include other functions that were previously developed?" : "A",
"What command is used to evaluate correct or incorrect response in this example?" : "C",
"Each 'if' command contains an '_________' to determine a true or false condition?" : "B"
}
# answers formatted into options that are in a double list
options = [["A. import", "B. libraries", "C. do it yourself", "D. cheat"],
["A. else", "B. boolean", "C. if", "D. for"],
["A. answer", "B. expression", "C. diet coke", "D. function"],
]
quiz()
print("Byeeeeee!")
Extra credit:
Yes I saw the repeating patterns of code. It was the checking answer part. I put the questions into a dictionary and put the options into a list. This allowed me to get the corerct answers and check it efficiently (by appending the key and values). I also made it so that the next question appears after you answer the previous one.