From 8bcd19afd005ee7b54f08b36f31cf80dcbd11d8f Mon Sep 17 00:00:00 2001 From: newt! Date: Tue, 14 Sep 2021 21:34:47 +0100 Subject: [PATCH] Add year 11 maths quiz --- .../year 11/Maths Quiz.py | 101 ++++++++++++++++++ readme.md | 4 + 2 files changed, 105 insertions(+) create mode 100644 coursework/gcse computer science/year 11/Maths Quiz.py diff --git a/coursework/gcse computer science/year 11/Maths Quiz.py b/coursework/gcse computer science/year 11/Maths Quiz.py new file mode 100644 index 0000000..3156946 --- /dev/null +++ b/coursework/gcse computer science/year 11/Maths Quiz.py @@ -0,0 +1,101 @@ +import random +import operator +import os + +# Constants +ANSWER_COUNT = 4 + +ops = { + '+': operator.add, + '-': operator.sub, + '*': operator.mul, + '/': operator.truediv +} + +score = 0 + +'''Clears the console.''' +def cls(): + os.system('cls' if os.name == 'nt' else 'clear') + +'''Formats a number.''' +def formatNumber(num): + if num % 1 == 0: + return str(int(num)) + else: + return '%.2f' % num + +'''Generates a question to be used''' +def generateQuestion(): + while True: + x = random.randint(0, 11) + y = random.randint(1, 11) + op = random.choice(list(ops.keys())) + if op == '/' and y > x: + continue + else: + answer = ops.get(op)(x, y) + return (x, y, op, answer) + +'''Generates a fake answer based on the real answer.''' +def generateFakeAnswer(answer): + r = random.randint(-10, 10) + return answer + r + +'''Asks a question.''' +def askQuestion(): + global score + x, y, op, answer = generateQuestion() + + # Generate a list of potential fake answers + answerList = {} + answerLocation = random.randint(1, ANSWER_COUNT) + + for i in range(1, 5): + if i == answerLocation: + answerList[i] = answer + else: + while True: + generated = generateFakeAnswer(answer) + if generated != answer: + answerList[i] = generated + break + + # Format that list of potential fake answers into a string + answers = '' + + for key in answerList: + value = answerList.get(key) + answers += '\n%i) %s' % (key, formatNumber(value)) + + # Ask the question + print(""" +What is the correct answer to the following expression? %i %s %i +%s + """ % (x, op, y, answers)) + + # Recieve input and mark the user based on it + while True: + try: + userAnswer = float(input('Which is the correct answer? ')) + if userAnswer == answerLocation: + print('You got it right!') + score = score + 1 + else: + print('You got it wrong!') + break + except ValueError: + print('Your input must be a number!') + +# Ask the user how many questions they would like to be asked +while True: + try: + howMany = int(input('How many questions would you like to answer? ')) + cls() + for i in range(howMany): + askQuestion() + cls() + print('Your final score is %i/%i!' % (score, howMany)) + break + except ValueError: + print('Your input must be a number!') diff --git a/readme.md b/readme.md index 0b6e5b4..8df6e45 100644 --- a/readme.md +++ b/readme.md @@ -54,6 +54,7 @@ - [Pet Planet](coursework/gcse%20computer%20science/year%209/python/web/pet%20planet) - [Year 10](coursework/gcse%20computer%20science/year%2010) + - [Calculator](coursework/gcse%20computer%20science/year%2010/calculator) - [Cat or Dog](coursework/gcse%20computer%20science/year%2010/cat%20or%20dog) - [Mark Analyser](coursework/gcse%20computer%20science/year%2010/mark%20analyser) @@ -63,3 +64,6 @@ - [Raspberry Pie Game](coursework/gcse%20computer%20science/year%2010/raspberry%20pie%20game) - [API Hangman](coursework/gcse%20computer%20science/year%2010/API%20Hangman.py) - [Temperature Bar Chart](coursework/gcse%20computer%20science/year%2010/Temperature%20Bar%20Chart.py) + +- [Year 11](coursework/gcse%20computer%20science/year%2011) + - [Maths Quiz](coursework/gcse%20computer%20science/year%2011/Maths%20Quiz.py)