DEV Community

oladejo abdullahi
oladejo abdullahi

Posted on • Edited on

How to make a game with python

simple game for kid

In maths class it will be more interesting if we have a kind of game that kids could play and still be learning from it. here I am going to show you how to write codes for multiplication game.
The nature of our game

  1. we need the screen to display question and ask the kid to provide the answer
  2. the question should be multiplication of two random number from 1 to 20
  3. it should state if the kid get it right or wrong
  4. repeat the question 10 times
  5. print the kids scores. Now that we have known the nature we need to interpret it in code form.
    • we need randint() to generate random number
    • input() and int() for kid to provide answer
    • for loop in range 10 to ask question 10times
    • if function to check if answer right or wrong
    • counter that keep the score record
    • print() function to print out the result. Now let's code it.
from random import randint #to import the randint
mark=0
for i in range(10):
    a=randint(1,20)#first number
    b=randint(1,13)#second number
    que=int(input('Question {}: {} x {} ='.format(i+1,a,b))) #format method to insert the question order
    if que==a*b: #check if the answer is correct
        print('Right!')
        mark=mark+1 #counter
    else:#if the answer wrong
        print('Wrong! The answer is', a*b)
print('You scored {}/{}'.format(mark,10)) 
#woow our game has been made
Enter fullscreen mode Exit fullscreen mode

Now run the code did you see how interesting it is? good! you can change the multiplication sign to plus,divide or minus to build different kind of game. did you enjoy it yeah, remember to like.

if you have any question don't hesitate to ask. chat me up on WhatsApp or Mail. Don't forget to follow me on Twitter so that you don't miss any of my articles.

Top comments (2)

Collapse
 
maxwizardth profile image
oladejo abdullahi

woow I have check it thanks ever so much

Collapse
 
seemasaharan profile image
Seema Saharan

Nice!