Awesome-Python-Scripts/Flash-card-Challenge/quizzer.py
Utkarsh Sharma 265d3c9eb7 Flash-card-Challenge
Python script that takes a file containing flash card questions and answers
as an argument and quizzes the user based on the contents of that file until
the user quits the program. Questions should be selected randomly (as opposed
to going in order through the file), and the user should type in their guess.
The script should say whether or not a guess is correct and provide the
correct answer if an incorrect answer is given.
2018-10-02 15:03:23 -07:00

24 lines
679 B
Python

import random
import argparse
parser = argparse.ArgumentParser(description="Get the quiz questions file")
parser.add_argument('file', help="a quiz file containing questions and answers")
args = parser.parse_args()
file = args.file
state_capitals = {}
with open(file) as f:
for line in f:
(key, val) = line.strip().split(',')
state_capitals[key] = val
while(True):
choice = random.choice(list(state_capitals.keys()))
answer = input(('{}? '.format(choice)))
if answer == state_capitals[choice]:
print("Correct! Nice job.")
elif answer.lower() == "exit":
print("Goodbye")
break
else:
print("Incorrect. The correct answer is {}".format(state_capitals[choice]))