Talk:Flash card challenge

From OpenHatch wiki
Jump to navigation Jump to search

from random import choice import argparse import sys

if len (sys.argv) == 1: ## The Error Handling! The len (sys.argv) being 1 (only the name of the running script) means that there has been no other argument

   print ("Oops! You forgot to provide a file for the questions and the answers. Please do it")
   sys.exit (0)

parser = argparse.ArgumentParser ("Path of the file containing the quiz") parser.add_argument ('file', type = str, help = "path of the file") args = parser.parse_args ()

if __name__ == "__main__":

   qa_file = open (args.file)
   qa_dico = {}
   for line in qa_file:        ## Loops through the file lines (stopping at \n)
       stripped_line = line.strip ()         ## First we remove the blank spaces
       qa_list = stripped_line.split (',', 2)      ## Then we split the newly stripped line into 2 parts separated by the coma
       qa_dico [qa_list [0]] = qa_list [1]         ## and we fill our dictionary
   print ()
   print ("Welcome to the flash card challenge!")
   print ()
   while True:
       qa_list = list (qa_dico.keys())     ## Converts the keys of the dictionary into a list
       question = choice (qa_list)
       print (question, "?", end = '   ')
       answer = input ("")
       if (answer == "exit" or answer == "EXIT" or answer == "close" or answer == "quit"):       ## To quit the program, one needs to type one of these words
           break
       elif (answer == qa_dico [question] or answer == qa_dico [question].lower () or answer == qa_dico [question].upper()):      ## Upper and Lower case should be tolerated
           print ('Correct!')            
       else:
           print ("Wrong answer! It is", qa_dico [question])
       print ()
   
   print ()
   print ("Thank's for playing! \n")
   print ("==> KaliPrime <==")
      
   qa_file.close ()