Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------
- QLIBRARY.PY
- ----------------------
- """
- PYTHON QUESTIONNAIRE LOADER
- FROM A JSON FILE
- """
- #Uses the json file questions.json
- import json,random #Importing needed libraries for these functions
- score = 0
- maxscore = 0 #Initiating score variables
- with open("questions.json") as data_file:
- data = json.load(data_file) #Loading the json file, and saving it as a Python dictionary
- def ix(self,dict,n): #A function to find what the nth key of a dictionary is
- count=0
- for i in sorted(dict.keys()):
- if n==count:
- return i
- else:
- count+=1
- def load(topic,q,a,trim,n): #The main load function, it parses the data
- 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
- q [y] = data[topic][y]["Q"] #Adds questions to a new dictionary
- a [y] = data[topic][y]["A"] #Adds the answers to a new dictionary
- if(trim==True): #If you want to shorten the answers
- x = len(q) #How many questions there are
- p = x - n #The difference between the chosen number of questions and the true number
- while(p >= 1):
- r = random.choice(list(q.keys())) #Choose a random key from the question dictionary
- q.pop(r)
- a.pop(r) #Remove that from both the question and answer dictionaries
- x = len(q)
- p = x - n #Re-calculate the difference
- def question(q,a,s): #The function to take the data from the new dictionaries, and output them as human-readable questions
- global score
- global maxscore
- att= 3 #The number of attempts remaining
- maxscore += 3 #Increases the maxscore by 3 per question
- while(att>0):
- x = input(q) #Takes the user input
- if(x==a): #If the answer is right
- print("Well done! You have got",att,"points\n")
- score += att
- break #Ends the loop, moves onto next question
- else:
- att -= 1 #If they are wrong, minus one from attempts
- if(att==0):
- print("The answer was",a,"\n") #If all attempts used, give the answer and no points
- def quiz(q,a): #A function to take the data from the dictionaries, and run the question function a relevant number of times
- for i in range(len(q)): #For the length of the trimmed question dictionary
- question(q[ix("",q,i)],a[ix("",a,i)],score) #Gives the right arguments for the question function
- print("Your score is:",str(score)+"/"+str(maxscore))
- ----------------------
- QUESTIONS.JSON
- ----------------------
- {
- "maths": [
- {
- "Q": "What is 1+1: ",
- "A": "2"
- },
- {
- "Q":"What is 2+2: ",
- "A":"4"
- },
- {
- "Q":"What is 8*12: ",
- "A":"96"
- },
- {
- "Q":"What is 2^3: ",
- "A":"8"
- },
- {
- "Q":"What is the square root of 225: ",
- "A":"15"
- },
- {
- "Q":"What is the cube root of 125: ",
- "A":"5"
- },
- {
- "Q":"What is the fourth root of 81: ",
- "A":"3"
- },
- {
- "Q":"What is 128/32: ",
- "A":"4"
- },
- {
- "Q":"What is 112-24: ",
- "A":"88"
- },
- {
- "Q":"If 2x+5=x+10, what is x: ",
- "A":"5"
- },
- {
- "Q":"What is 99/10: ",
- "A":"9.9"
- }
- ],
- "ICT": [
- {
- "Q":"What is integer shortened to: ",
- "A":"int"
- },
- {
- "Q":"What is string shortened to: ",
- "A":"str"
- }
- ]
- }
- ----------------------
- QUIZ.PY
- ----------------------
- import qlibrary as quiz #Imports all the needed quiz functions
- print("Please answer in all lowercase letters")
- a,q = {},{} #Initiates the dictionaries
- print("Please choose one of these subjects:")
- print("1) Maths")
- print("2) Computer science")
- subject = int(input("Option:")) #Chooses which questions to answer
- questions = int(input("How many questions would you like to answer (Max of 10):")) #Chooses how many questions to answer
- print("\n")
- if(subject==1):
- quiz.load("maths",q,a,True,questions) #Loads the needed json data
- quiz.quiz(q,a) #Starts all the question functions
- elif(subject==2):
- quiz.load("ICT",q,a,True,questions)
- quiz.quiz(q,a)
- else:
- print("u wot")
Advertisement
Add Comment
Please, Sign In to add comment