import time # dictionary with questions and answer q_a = { 'Q 1: Which data structure is immutable?': ['A: tulip', 'B: tuple', 'C: tumble', 'B'], 'Q 2: Which data structure does not allow duplicate objects?': ['A: set', 'B: list', 'C: dictionary', 'A'], 'Q 3: How do you sort a dictionary?':['A: sort(dict)', 'B: sorted(dict)', 'C: dict.sorted', 'B'], 'Q 4: How do you remove the 5 from list = [5,6,7,8]?': ['A: list.remove(0)', 'B: list.remove(5)', 'C: list.remove(1)', 'B'], 'Q 5: Which keyword do you use to loop over a given list of elements?': ['A: for', 'B: while', 'C: loop', 'A'], 'Q 6: Which command do you use to access modules?':['A: load', 'B: source', 'C: import', 'C'], 'Q 7: What characterises suits of code?':['A: indentation and preceding colon', 'B: curved brackets and preceding colon', 'C: indentation followed by semicolon', 'A'], 'Q 8: How do you get to a function\'s documentation?':['A: help(function_name)', 'B: help.function_name', 'C: ?function_name', 'A'], 'Q 9: What is an object in Python?':['A: data structures and variables', 'B: data structures and functions', 'C: everything', 'C'], 'Q10: What does found={} represents?':['A: an empty set', 'B: an empty dictionnary', 'C: both an empty set or an empty dictionnary', 'B'], 'Q11: Which keyword do you use to name a function?':['A: function', 'B: def', 'C: fun', 'B'], 'Q12: How do you add a multiline comment in your programm?':['A: """ comment """', 'B: # comment', 'C: # comment #', 'A'], 'Q13: What does not hold for Python?':['A: Don\'t mix tabs with spaces in your Python code.', 'B: A few lines of code do a lot.', 'C: Don\'t mix and match the data structures.', 'C', 'end']} # set of valid answers set_answer = {'A','B','C'} # initialise num_correct=number of correct answers num_correct = 0 # loop across questions (sorted) for k, v in sorted(q_a.items()): # print question print(k) # print possible answers for i in range(3): print(v[i]) # ask for answer answer = input('Please choose your answer (A,B or C):') # check if answer is valid (loop until answer is valid): while answer not in set_answer: answer = input('This answer is invalid. Please choose A, B or C:') # check if answer is correct: if answer == v[3]: print('Correct!') num_correct = num_correct + 1 else: print('Not correct. The correct answer is ', v[3],'.') time.sleep(1) print() if len(v) == 4: input('Please press Enter to continue.') print() # final quiz result print('You answered', num_correct, 'of', len(q_a), 'questions correctly.') if num_correct/len(q_a) < 0.33: print('Perhaps you should read the book again.') elif num_correct/len(q_a) <0.6: print('Not bad, but you might want to re-read some sections of the book.') elif num_correct/len(q_a) >0.9: print('You\'re an expert!') else: print('Well done!') time.sleep(3) print() input('Please press Enter to exit the programme.')