This is a Python program aimed at recalling and improving the overall memory of the individual using a series of random words and numbers.
At first, the main program imports all the necessary libraries and files combined with an ASCII art logo. Then according to the user input, it calls the necessary functions.
import wordzz
import numberzz
import countrizz
import flowerzz
import random
import os
import time
import sys
import logo
print(logo.memso)
print("Welcome to the Memory Game: MEMSO \n")
def choices():
types = input("Choose either 'Words, 'Countries', 'Flowers' or 'Numbers' \n")
types = types.lower()
if (types == 'numbers'):
numberzz.num_question()
numberzz.num_answer()
elif (types == 'words'):
wordzz.question_word()
wordzz.answer_word()
elif (types == 'countries'):
countrizz.question_word()
countrizz.answer_word()
elif (types == 'flowers'):
flowerzz.question_word()
flowerzz.answer_word()
else:
print("You gave an invalid choice.")
choices()
choices()
Letβs continue to the file that is responsible for handling the word logic. In the beginning, we read the words file, and based on the user input a predefined value is allocated according to the specified value. There is an option for a custom input, that is a specific number of words to be presented and recalled at a later stage. When the words finish showing up, then the user has to recall them in the exact order to get 1 point, otherwise, it loses 1 point. Finally, the overall score is shown at the end.
import random
import time
import os
import sys
f = open('words.txt', 'r')
content = f.read()
words = content.split("\n")
def question_word():
choice = input("Choose either 'Level' or 'Specific' \n")
if (choice == "level"):
choice = choice.lower()
level = input("Enter Level # Low / Medium / Hard \n")
if (level == 'low'):
question_word.num_words = 5
elif (level == 'medium'):
question_word.num_words = 10
elif (level == 'hard'):
question_word.num_words = 20
else:
print(f"Invalid entry.")
question_word()
answer_word()
elif (choice == 'specific'):
choice = choice.lower()
question_word.num_words = int(input("Enter how many words to recall: \n"))
else:
print(f"Invalid entry.")
question_word()
answer_word()
print(f"After 5 seconds, you will see a list of {question_word.num_words} random words. \n")
print("You have to recall them in the correct order. \n")
time.sleep(5)
os.system('clear')
question_word.answer_words = []
for word in range(0, question_word.num_words):
random_word = random.randint(0, len(words) - 1)
print(f"Word[{word + 1}]: {words[random_word]}")
time.sleep(1)
os.system('clear')
question_word.answer_words.append(words[random_word])
os.system('clear')
print("Time to recall the words in order: \n")
def answer_word():
score = 0
for answer in range(0, len(question_word.answer_words)):
userAnswer = input(f"Please type the {answer + 1} word: \n")
userAnswer = userAnswer.lower()
os.system('clear')
if (userAnswer == question_word.answer_words[answer].lower()):
print(f"Correct (+1)")
score = score + 1
else:
print(f"Wrong (-1): The word was: {question_word.answer_words[answer]}")
score = score - 1
print(f"Your total score: {score}")
def continues_word():
continues = input(f"Type 'Y' if you want to start over or 'N' if you want to terminate the program. \n")
continues = continues.lower()
if (continues == 'y'):
question_word()
answer_word()
elif (continues == 'n'):
sys.exit()
else:
continues_word()
continues_word()
f.close()
Exactly, the same approach is being used for Countries and Flowers, with the only difference of changing the file containing the appropriate words. Here is the code for both of these files.
import random
import time
import os
import sys
f = open('countries.txt', 'r')
content = f.read()
words = content.split("\n")
def question_word():
choice = input("Choose either 'Level' or 'Specific' \n")
if (choice == "level"):
choice = choice.lower()
level = input("Enter Level # Low / Medium / Hard \n")
if (level == 'low'):
question_word.num_words = 5
elif (level == 'medium'):
question_word.num_words = 10
elif (level == 'hard'):
question_word.num_words = 20
else:
print(f"Invalid entry.")
question_word()
answer_word()
elif (choice == 'specific'):
choice = choice.lower()
question_word.num_words = int(input("Enter how many countries to recall: \n"))
else:
print(f"Invalid entry.")
question_word()
answer_word()
print(f"After 5 seconds, you will see a list of {question_word.num_words} random countries. \n")
print("You have to recall them in the correct order. \n")
time.sleep(5)
os.system('clear')
question_word.answer_words = []
for word in range(0, question_word.num_words):
random_word = random.randint(0, len(words) - 1)
print(f"Word[{word + 1}]: {words[random_word]}")
time.sleep(1)
os.system('clear')
question_word.answer_words.append(words[random_word])
os.system('clear')
print("Time to recall the countries in order: \n")
def answer_word():
score = 0
for answer in range(0, len(question_word.answer_words)):
userAnswer = input(f"Please type the {answer + 1} country: \n")
userAnswer = userAnswer.lower()
os.system('clear')
if (userAnswer == question_word.answer_words[answer].lower()):
print(f"Correct (+1)")
score = score + 1
else:
print(f"Wrong (-1): The country was: {question_word.answer_words[answer]}")
score = score - 1
print(f"Your total score: {score}")
def continues_word():
continues = input(f"Type 'Y' if you want to start over or 'N' if you want to terminate the program. \n")
continues = continues.lower()
if (continues == 'y'):
question_word()
answer_word()
elif (continues == 'n'):
sys.exit()
else:
continues_word()
continues_word()
f.close()
import random
import time
import os
import sys
f = open('flowers.txt', 'r')
content = f.read()
words = content.split("\n")
def question_word():
choice = input("Choose either 'Level' or 'Specific' \n")
if (choice == "level"):
choice = choice.lower()
level = input("Enter Level # Low / Medium / Hard \n")
if (level == 'low'):
question_word.num_words = 5
elif (level == 'medium'):
question_word.num_words = 10
elif (level == 'hard'):
question_word.num_words = 20
else:
print(f"Invalid entry.")
question_word()
answer_word()
elif (choice == 'specific'):
choice = choice.lower()
question_word.num_words = int(input("Enter how many flowers to recall: \n"))
else:
print(f"Invalid entry.")
question_word()
answer_word()
print(f"After 5 seconds, you will see a list of {question_word.num_words} random flowers. \n")
print("You have to recall them in the correct order. \n")
time.sleep(5)
os.system('clear')
question_word.answer_words = []
for word in range(0, question_word.num_words):
random_word = random.randint(0, len(words) - 1)
print(f"Word[{word + 1}]: {words[random_word]}")
time.sleep(1)
os.system('clear')
question_word.answer_words.append(words[random_word])
os.system('clear')
print("Time to recall the flowers in order: \n")
def answer_word():
score = 0
for answer in range(0, len(question_word.answer_words)):
userAnswer = input(f"Please type the {answer + 1} flower: \n")
userAnswer = userAnswer.lower()
os.system('clear')
if (userAnswer == question_word.answer_words[answer].lower()):
print(f"Correct (+1)")
score = score + 1
else:
print(f"Wrong (-1): The flower was: {question_word.answer_words[answer]}")
score = score - 1
print(f"Your total score: {score}")
def continues_word():
continues = input(f"Type 'Y' if you want to start over or 'N' if you want to terminate the program. \n")
continues = continues.lower()
if (continues == 'y'):
question_word()
answer_word()
elif (continues == 'n'):
sys.exit()
else:
continues_word()
continues_word()
f.close()
Finally, we are at the numbers section. We create a list that contains all numbers, from 0 to 9. Then, the user is prompted to enter how many digits to remember. A random digit is presented each time, and when all digits are shown, then the user must type them in the exact order. If they recalled correctly, they get 1 point, otherwise -1 point. Lastly, a statistics table is presented, showing the number of times each number is presented.
import random
import time
import os
import sys
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
def num_question():
num_question.num_numbers = int(input("Enter how many digits to recall: \n"))
print(f"After 5 seconds, you will see a list of {num_question.num_numbers} random digits. \n")
print("You have to recall them in the correct order. \n")
time.sleep(5)
os.system('clear')
num_question.answer_digits = []
for number in range(0, num_question.num_numbers):
random_digit = random.randint(0, len(numbers) - 1)
print(f"Digit[{number + 1}]: {numbers[random_digit]}")
time.sleep(1)
os.system('clear')
num_question.answer_digits.append(numbers[random_digit])
def statistics():
print(f"-- Statistics -- \n")
for i in range(0, 10):
count = num_question.answer_digits.count(i)
print(f"{i} appeared: {count} times")
def num_answer():
score = 0
for answer in range(0, len(num_question.answer_digits)):
userAnswer = input(f"Please type the {answer + 1} digit: \n")
os.system('clear')
if (userAnswer == str(num_question.answer_digits[answer])):
print(f"Correct (+1)")
score = score + 1
else:
print(f"Wrong (-1): The digit was: {num_question.answer_digits[answer]}")
score = score - 1
print(f"Your total score: {score}")
statistics()
def continues_num():
continues = input(f"Type 'Y' if you want to start over or 'N' if you want to terminate the program. \n")
continues = continues.lower()
if (continues == 'y'):
num_question()
num_answer()
statistics()
elif (continues == 'n'):
sys.exit()
else:
continues_num()
continues_num()
git clone https://github.com/mariosdaskalas/memso
cd memso/
python3 main.py
Or a one-liner command.
git clone https://github.com/mariosdaskalas/memso && cd memso/ && python3 main.py
You can find the Source Code on Github.
Image by Gerd Altmann from Pixabay
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
Comments