element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi Projects
  • Products
  • Raspberry Pi
  • Raspberry Pi Projects
  • More
  • Cancel
Raspberry Pi Projects
Blog Math game with Raspberry Pi Pico
  • Blog
  • Documents
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: bidrohini
  • Date Created: 23 Mar 2023 2:52 PM Date Created
  • Views 1546 views
  • Likes 9 likes
  • Comments 3 comments
Related
Recommended

Math game with Raspberry Pi Pico

bidrohini
bidrohini
23 Mar 2023
Math game with Raspberry Pi Pico

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.


   image

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:

image    



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.                          
  • Sign in to reply
  • javagoza
    javagoza over 2 years ago

    Several years ago one of my sons made this version with Scratch:

    https://scratch.mit.edu/projects/54591368/

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • scottiebabe
    scottiebabe over 2 years ago

    love it! 100

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • dougw
    dougw over 2 years ago

    I had a teacher in grade 8 that would randomly ask us questions like that. Those skills turned out to be useful, although it wasn't a game with her.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube