GandalfTheCat

Untitled

Apr 5th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.24 KB | None | 0 0
  1. ----------------------
  2. QLIBRARY.PY
  3. ----------------------
  4. """
  5. PYTHON QUESTIONNAIRE LOADER
  6. FROM A JSON FILE
  7. """
  8.  
  9. #Uses the json file questions.json
  10.  
  11. import json,random #Importing needed libraries for these functions
  12.  
  13. score = 0
  14. maxscore = 0 #Initiating score variables
  15.  
  16.  
  17. with open("questions.json") as data_file:
  18.     data = json.load(data_file) #Loading the json file, and saving it as a Python dictionary
  19.  
  20.  
  21. def ix(self,dict,n): #A function to find what the nth key of a dictionary is
  22.     count=0
  23.     for i in sorted(dict.keys()):
  24.         if n==count:
  25.             return i
  26.         else:
  27.             count+=1
  28.  
  29. def load(topic,q,a,trim,n): #The main load function, it parses the data
  30.     for y in range(0,len(data[topic])): #Runs all the code in this for as many elements are nested inside the specific topic in the json file
  31.         q [y] = data[topic][y]["Q"] #Adds questions to a new dictionary
  32.         a [y] = data[topic][y]["A"] #Adds the answers to a new dictionary
  33.     if(trim==True): #If you want to shorten the answers
  34.         x = len(q) #How many questions there are
  35.         p = x - n #The difference between the chosen number of questions and the true number
  36.         while(p >= 1):
  37.             r = random.choice(list(q.keys())) #Choose a random key from the question dictionary
  38.             q.pop(r)
  39.             a.pop(r) #Remove that from both the question and answer dictionaries
  40.             x = len(q)
  41.             p = x - n #Re-calculate the difference
  42.  
  43. def question(q,a,s): #The function to take the data from the new dictionaries, and output them as human-readable questions
  44.     global score
  45.     global maxscore
  46.     att= 3 #The number of attempts remaining
  47.     maxscore += 3 #Increases the maxscore by 3 per question
  48.     while(att>0):
  49.         x = input(q) #Takes the user input
  50.         if(x==a): #If the answer is right
  51.             print("Well done! You have got",att,"points\n")
  52.             score += att
  53.             break #Ends the loop, moves onto next question
  54.         else:
  55.             att -= 1 #If they are wrong, minus one from attempts
  56.     if(att==0):
  57.         print("The answer was",a,"\n") #If all attempts used, give the answer and no points
  58.  
  59.  
  60. def quiz(q,a): #A function to take the data from the dictionaries, and run the question function a relevant number of times
  61.     for i in range(len(q)): #For the length of the trimmed question dictionary
  62.         question(q[ix("",q,i)],a[ix("",a,i)],score) #Gives the right arguments for the question function
  63.     print("Your score is:",str(score)+"/"+str(maxscore))
  64.  
  65. ----------------------
  66. QUESTIONS.JSON
  67. ----------------------
  68. {
  69.   "maths":  [
  70.     {
  71.       "Q": "What is 1+1: ",
  72.       "A": "2"
  73.     },
  74.     {
  75.       "Q":"What is 2+2: ",
  76.       "A":"4"
  77.     },
  78.     {
  79.       "Q":"What is 8*12: ",
  80.       "A":"96"
  81.     },
  82.     {
  83.       "Q":"What is 2^3: ",
  84.       "A":"8"
  85.     },
  86.     {
  87.       "Q":"What is the square root of 225: ",
  88.       "A":"15"
  89.     },
  90.     {
  91.       "Q":"What is the cube root of 125: ",
  92.       "A":"5"
  93.     },
  94.     {
  95.       "Q":"What is the fourth root of 81: ",
  96.       "A":"3"
  97.     },
  98.     {
  99.       "Q":"What is 128/32: ",
  100.       "A":"4"
  101.     },
  102.     {
  103.       "Q":"What is 112-24: ",
  104.       "A":"88"
  105.     },
  106.     {
  107.       "Q":"If 2x+5=x+10, what is x: ",
  108.       "A":"5"
  109.     },
  110.     {
  111.       "Q":"What is 99/10: ",
  112.       "A":"9.9"
  113.     }
  114.   ],
  115.   "ICT": [
  116.     {
  117.       "Q":"What is integer shortened to: ",
  118.       "A":"int"
  119.     },
  120.     {
  121.       "Q":"What is string shortened to:  ",
  122.       "A":"str"
  123.     }
  124.   ]
  125. }
  126.  
  127. ----------------------
  128. QUIZ.PY
  129. ----------------------
  130. import qlibrary as quiz #Imports all the needed quiz functions
  131.  
  132. print("Please answer in all lowercase letters")
  133. a,q = {},{} #Initiates  the dictionaries
  134.  
  135. print("Please choose one of these subjects:")
  136. print("1) Maths")
  137. print("2) Computer science")
  138. subject = int(input("Option:")) #Chooses which questions to answer
  139.  
  140. questions = int(input("How many questions would you like to answer (Max of 10):")) #Chooses how many questions to answer
  141.  
  142. print("\n")
  143. if(subject==1):
  144.     quiz.load("maths",q,a,True,questions) #Loads the needed json data
  145.     quiz.quiz(q,a) #Starts all the question functions
  146. elif(subject==2):
  147.     quiz.load("ICT",q,a,True,questions)
  148.     quiz.quiz(q,a)
  149. else:
  150.     print("u wot")
Advertisement
Add Comment
Please, Sign In to add comment