Have you played number games with ChatGPT? It can play some math games with you. In earlier days of its development, one of such games were the math solving game. It asked you arithmetical questions one by one. And you had to answer. It could tell you if the answer was correct or wrong.
I and my kid enjoyed this game. I decided to play this game with Raspberry Pi Pico. So, I wrote this code using Thonny. I used Pico but you can play the same game with your PC. It's just some basic arithmetical operations using python. The same type of code that you write for making a calculator.
How does the program work?
It picks up two random numbers. It also picks up an operator randomly. Thus it prints a mathematical problem. Then it asks result to the user. If the user gives correct answer, it says 'Right answer'. If the answer is wrong, Pico says sorry and also lets the user know the right answer.
Scopes for modification:
Currently I am connecting my Pico to my laptop. Pico is printing questions to the Thonny shell. And it is accepting answers from Laptop keyboard. I really want to modify this project. I want to add a keypad and LCD to omit the laptop screen and keyboard. Though I used Pico, Keypad and LCD together in my other projects, I cannot use them successfully for this project yet. I wish to to that soon. For this time being, I am sharing the code I have written so far.
Hardware used:
Raspberry Pi Pico with header x 1
MicroUSB cable x 1
IDE used:
Thonny IDE
Installing Thonny and programming Raspberry Pi Pico with it:
If you still do not have Thonny in your computer, you can follow this tutorial. This will help you know the A to Z of how to install thonny and how to program Raspberry Pi Pico for the first time and later.
Code:
I wrote the following code in Thonny:
import random Operator_list = ['+', '-', '*', '/'] print("Let's play number game") while(1): random_number1 = random.randint(0, 9) random_number2 = random.randint(0, 9) operator = random.choice(Operator_list) if(operator=='+'): result=random_number1+random_number2 elif(operator=='-'): result=random_number1-random_number2 elif(operator=='*'): result=random_number1*random_number2 elif(operator=='/'): result=random_number1/random_number2 print((random_number1),operator,(random_number2),'=','?') answer=input() answer=float(answer) if(answer==result): print("Right answer") else: print("Sorry.The answer is",result)
After running the code, Pico starts playing with you.
Note:
You can adjust the difficulty level of your questions by increasing or decreasing the range of numbers in these two lines:
random_number1 = random.randint(0, 9) random_number2 = random.randint(0, 9)
For example, I changed these two lines to:
random_number1 = random.randint(0, 99) random_number2 = random.randint(0, 99)
And the output was like this:
I think it's a good game. Specially for school going kids who are learning maths. Hopefully, someday I will be able to do it
with an LCD and keypad and share my code with all of you. Till then, bye.