Made this generator to practice using imports from other modules and better readability for coding. What could I have done better and what did I do wrong?
File called test_generator.py
from test_dictionary import math as Math, trivia as Trivia
import random
math = list(Math.keys())
trivia = list(Trivia.keys())
def getQuestions(question_type):
question_list = []
for i in range(random.randint(1,5)):
random_index = random.randint(0, len(question_type) - 1)
question_list.append(question_type[random_index])
return question_list
def answerChecker(question_list, answer_list, question_type):
correct_answer = 0
for index in range(len(question_list)):
if question_type[question_list[index]] == answer_list[index]:
correct_answer += 1
return correct_answer
def questionPrinter():
for question in math_questions:
print('\n' + question)
math_answers.append(input('Answer? ').lower())
for question in trivia_questions:
print('\n' + question)
trivia_answers.append(input('Answer? ').lower())
math_questions = getQuestions(math)
trivia_questions = getQuestions(trivia)
math_answers = []
trivia_answers = []
questionPrinter()
correct_answer = answerChecker(math_questions, math_answers, Math)
correct_answer += answerChecker(trivia_questions, trivia_answers, Trivia)
print(f'{correct_answer}/{len(math_questions) + len(trivia_questions)}')
File called test_dictionary.py
math = {
'What is the square root of 121?': '11',
'Who created the Pythogorean theorem?': 'pythagoras',
'What is 4 cubed?': '64',
'550 / 5? ': '110',
'If 1=3, 2=3, 3=5. 4=4, 5=4, Then 6 = what? ': '3',
'8.563 + 4.8292? ': '13.3922',
'I am an odd number. Take away one letter and I become even. What number am I? ' : '7',
'Sally is 54 years old and her mother is 80, how many years ago was Sally’s mother times her age? ': '41',
'There is a three-digit number. The second digit is four times as big as the third digit, while the first digit is three less than the second digit. What is the number? ': '141',
'How many feet are in a mile? ': '5280',
'Solve -15 + (-5x) = 0': '-3',
'What is 1.92÷3': '0.64',
'If 72 x 96 = 6927, 58 x 87 = 7885, then 79 x 86 = what? ': '6897',
'Look at this series: 36, 34, 30, 28, 24, … What number should come next? ': '22',
'Look at this series: 22, 21, 23, 22, 24, 23, … What number should come next? ': '25',
'If 13 x 12 = 651 & 41 x 23 = 448, then, 24 x 22 = what? ': '924',
'Look at this series: 53, 53, 40, 40, 27, 27, … What number should come next? ': '14'
}
trivia = {
'What was the 43 state to join the USA? ': 'idaho',
'What year was the very first model of the iPhone released? ': '2007',
'Is Java a type of OS? ': 'no',
'Which email service is owned by Microsoft? ': 'hotmail',
'What was Twitter’s original name? ': 'twttr',
'What is the symbol for potassium? ': 'k',
'Which planet has the most gravity? ': 'jupiter',
'Which animal can be seen on the Porsche logo? ': 'horse',
'Which company owns Bugatti, Lamborghini. Audi, Porsche, and Ducati? ': 'volkswagen',
'In what year was the Corvette introduced? ': '1953',
'How many soccer players should each team have on the field at the start of each match? ': '11',
'Which country invented tea? ': 'china',
'Which country is responsible for giving us pizza and pasta? ': 'italy',
'What is your body’s largest organ? ': 'skin',
'What kind of cells are found in the brain? ': 'neurons',
'Which continent is the largest? ': 'asia',
'In which year did World War I begin?': '1914',
}
Unused file called test_grabber - I used this one to web scrape math questions, but at the end of the day I would still have to edit/change most of the questions, so I tpyed(ctrl+c and ctrl+v) the questions myself with minimal tweaking.
import bs4
import requests
request = requests.get('https://www.gkgigs.com/maths-general-knowledge-questions-and-answers/#:~:text=Maths%20GK%20Questions%20And%20Answers%201%201.%20Who,of%20the%20lever%20and%20pulley%3F%20...%20More%20items').text
website_html_content = bs4.BeautifulSoup(request,'lxml')
website_questions = website_html_content.select('.schema-faq-question')
website_answers = website_html_content.select('.schema-faq-answer')
def creating_question_list():
all_questions = []
for question in website_questions:
all_questions.append(question.text)
return all_questions
def creating_answer_list():
all_answers = []
for answer in website_answers:
all_answers.append(answer.text[8::])
return all_answers
all_questions = creating_question_list()
all_answers = creating_answer_list()
print(all_answers)
with open('Math_Questions', 'w+') as f:
f.write('math = {\n')
for index in range(len(all_questions)):
f.write(f"\t'{all_questions[index]}' : '{all_answers[index]}',\n")
f.write('}')