Skip to content

Instantly share code, notes, and snippets.

@anomalyjane
Last active August 21, 2016 00:35
Show Gist options
  • Save anomalyjane/2f42021e677c5fcd2dbee4c329d22c82 to your computer and use it in GitHub Desktop.
Save anomalyjane/2f42021e677c5fcd2dbee4c329d22c82 to your computer and use it in GitHub Desktop.
A vocabulary quiz written for Python 2
{
"easy" :[
{"question" : "{} is a popular programming language that boasts high-readability code. You might think it was named after a snake species, but it was actually named after a British comedy act.", "answer" : "python"},
{"question" : "The python interpreter can interpret and manipulate various types of data. A whole number is referred to as an {}, while a decimal is known as a float.", "answer" : "integer"},
{"question" : "Another data type is called a {}. It can contain letters, words, sentences, or other characters saved as text.", "answer" : "string"},
{"question" : "Another useful data type in python is a {}, which stores a sequence of values separated by commas.", "answer": "list"}
],
"medium" :[
{"question" : "One of the first steps when learning to code is to learn how to assign a value to a {}, which then stores that value in the computer's memory.", "answer" : "variable"},
{"question" : "Next you will probably want to learn how to use simple {}s such as +, -, *, /, >, <, ==, and !=.", "answer" : "operator"},
{"question" : "Once you've mastered variables and operators, it's time to start learning how to write some simple {}s (aka functions), which consist of lines of code that run in a specified order and can eliminate redundancy.", "answer" : "procedure"},
{"question" : "A procedure takes one or more {}s as arguments, and returns one or more outputs.", "answer" : "input"}
],
"hard" :[
{"question" : "One important programming construct is called an {} statement; it tests whether a condition is true, and if so it may execute a block of code.", "answer" : "if"},
{"question" : "Another important construct used especially when writing procedures is a {}, which allows you to keep running the same code repeatedly under specified conditions.", "answer" : "loop"},
{"question" : "There are two common types of these repeating operations; one is called a {} loop; it runs through all of the values in a specified range.", "answer" : "for"},
{"question" :"The other is called a {} loop; this one runs repeatedly as long as a specified condition is true.", "answer" : "while"}
]
}
#!/usr/bin/env python
# This program is a fill-in-the blank quiz, prompting for user input at each blank.
import os
import json
with open('questions_and_answers.json', 'r') as f:
content = f.read()
content_dict = json.loads(content)
def evaluate(user_input, correct_answer):
#procedure that evaluates user response
if user_input.lower() == correct_answer:
return True
return False
def quiz_summary(errors):
#print out a final message at the end of the quiz
accuracy = '{0:.2f}'.format(4.0/(errors + 4) * 100)
print "{} {} {}".format("\nCongratulations! You survived the quiz. Your accurracy was",
accuracy, "percent.\n")
last_input = raw_input("How was your experience? ")
print "{} {} {}".format("\nGlad to hear it was", last_input.lower(), ". See you later!")
def clear_terminal():
os.system('cls' if os.name == 'nt' else 'clear')
def set_difficulty():
#difficulty is set through user input
difficulty = raw_input("\nTo select the difficulty setting, please enter easy, medium, or hard: ")
while difficulty not in content_dict:
difficulty = raw_input("Please enter a valid difficulty level: ")
return difficulty
def run_quiz():
clear_terminal()
print "Welcome to the quiz!"
difficulty = set_difficulty()
score = 0
errors = 0
clear_terminal()
for i, question_answer in enumerate(content_dict[difficulty]):
question = question_answer["question"]
answer = question_answer["answer"]
print "{0} {1} {2}{2}{3}".format("Question", str(i+1), "\n", question.format("__"+str(i+1)+"__"))
#appropriate prompt for response printed based on index
user_input = raw_input("\nType the answer for question " + str(i+1) + ": ")
if evaluate(user_input, answer):
score += 1
print "\n\n\n\n\nGreat!\n\n" + question.format(answer) + "\n\nYour score is " + str(score)
clear_terminal()
else:
errors += 1
print "\nNope! Try again."
quiz_summary(errors)
run_quiz()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment