-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrain_bee_game.py
100 lines (92 loc) · 4.45 KB
/
brain_bee_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Brainbeez in Python:
Text-based game that presents neuroscience questions to
an user to practice and improve their neuroscience knowledge,
especially as the user prepares to a Brain Bee Competition
Author: Juan C. Sanchez-Arias. (https://www.github.com/juansamdphd/brain_bee_game/)
First version: 2021.
License; GPL.
"""
import csv
import random
def main():
# print welcoming message
print("Welcome to Brainbeez! A text-based game to practice your neuroscience knowledge")
# get user answer on what topic to review
topic = get_user_topic()
print("You've chosen", topic + ".\n" + "To change topic, just type 'topic' as your answer to any of the questions.\nType 'exit' to leave.")
print("")
# print question from selected topic
question_list = get_questions(topic)
while question_list:
question = ask_question(question_list)
if question == 'topic':
print("")
topic = get_user_topic()
print("You've chosen", topic + ".\n" + "To change topic, just type 'topic' as your answer to any of the questions.\nType 'exit' to leave.")
print("")
elif question == 'exit':
break
elif not question_list:
print("That's all we have! Make sure to check BrainBeez again for more content.\nBrainBeez will be in constatn/regular development, so keep an eye out for future features and more questions!")
# print farewell message
print("")
print("Thanks for playing Brainbeez!\n")
print("To learn more about the Brain Bee go to https://www.thebrainbee.org/\nor check your regional/local brain bee website.\n")
print("Come back to practice some more any time you want\n")
def get_user_topic():
# gets user topic to practice
topic = int(input("What do you want to practice or learn about today?\n1. History of Neuroscience\n2. Neuroanatomy\n3. Neurobiology\n4. Clinical neuroscience\n\nEnter your answer (just type the corresponding number or 0 to exit): "))
while True: # as topics get added this value can change
if topic > 4:
topic = int(input("Invalid numner. Enter a valid number to select a topic or 0 to exit: "))
if topic == 1:
# get a history question
topic = "history"
return topic
elif topic == 2:
# get a neuroanatomy question
topic = "neuroanatomy"
return topic
elif topic == 3:
# get a neurobiology question
topic = "neurobiology"
return topic
elif topic == 4:
# get a clinical question
topic = "clinical"
return topic
elif topic == 0:
topic = "to exit"
return topic
def get_questions(topic):
# opens text file with questions from a tab delimited file
with open("brainbeez_questions.txt") as file:
question_bank = csv.DictReader(file, delimiter='\t') # uses csv.DictReader to make first line as headers and defines the delimiter as tab
# Get list of questions
question_list = get_topic_question_list(question_bank, topic)
return question_list
def get_topic_question_list(question_bank, topic):
# creates an empty ist to append available quesitons which topic matches the one selected by the user.
question_list = []
for line in question_bank: # evaluates each line from question_bank
# if the question topic matches the topic selected by the user, appends the question (a dictionary) to question_list
if line['topic'] == topic:
question_list.append(line)
return question_list
def ask_question(question_list):
# gets a random uestion from question_list and pops it out of the list, returns que the popped question as per pop()
question = question_list.pop(random.randrange(len(question_list)))
# Ask question to user and record answer
user_answer = input(question['question'] + " (hit enter to evaluate the answer): ")
# Compare user_answer with question answer
if user_answer.lower() == question['answer'].lower(): # compared as lowercase in case there is mix of upper and lower case
print("")
print("Yes! This is the correct answer!\n")
elif user_answer.lower() == 'topic' or user_answer.lower() == 'exit': # adds option to exit or change topic of practice
return user_answer
else:
print("")
print("Incorrect answer. The answer is", question['answer'],"\n")
if __name__ == "__main__":
main()