Introduction: Coding an Addition Game in Python

  • This instruction set will teach you step-by-step how to program an addition game that prompts users to answer simple addition problems using random numbers from 0-9 and prints whether they are correct or not!
  • Click the image in each step to enlarge it and view the code for that part.

Step 1: Launch Your Python Coding App.

  • This instruction set will be using the IDLE Python program!
  • After launching, create a New File in your Python application to start coding.

Step 2: Import the Random Class.

  • We will be using it to generate random numbers!

Step 3: Define a Python Method With an Input Variable N.

  • The input of integer n will determine the number of addition problems the game will print when called!
  • This code calls the method "game(n)".

Step 4: Initialize a Boolean Variable and an Integer Variable.

  • Within the game method, initialize a Boolean variable to be used in a ‘while’ loop and an integer to be used as a count variable for correct answers.
  • This code calls the Boolean “wrk” and integer “cnt”.
  • Remember the importance of indents in Python, as they determine what code is nested where!

Step 5: Start a ‘for’ Loop for Range N.

  • This will loop for the length of the input integer n!

Step 6: Initialize Two Random Integer Values Between 1 and 10 and Set Boolean Value to True.

  • Within this ‘for’ loop, use random.randrange(1,10) to initialize two random integer values between 1 and 9.
  • This code calls these “val1” and “val2”.
  • Then set the Boolean value to True!

Step 7: Start a ‘while’ Loop While the Boolean Variable Is True.

  • While still within the ‘for’ loop, start a ‘while’ loop while the Boolean variable is True.

Step 8: Print an Addition Problem With Values 1 and 2 and Take the Answer As Input.

  • Next in this ‘while’ loop, we create a try-except statement.
  • In your ‘try’ case, print out an addition question using value 1 and value 2 and define an answer variable as the user’s input (this code defines the answer variable as “ans”).

Step 9: Make an If-else Statement Testing Whether Answer = Value 1 + Value 2.

  • Within the ‘try’ case, code an if-else statement testing whether ans = val1 + val2.

Step 10: If True, Print a Correct Message, Set the Boolean Variable to False, and Increment Count.

  • Still within the 'try' statement, if true:
    • Print a correct message!
    • Set the Boolean variable to False!
    • Increment count by 1!

Step 11: If Not, Print an Incorrect Message and Set the Boolean Value to False.

  • In the 'else' statement, print an incorrect message and set the Boolean value to False.

Step 12: Account for Non-integer Inputs With an Error Message.

  • In the ‘except’ case, print an error message to account for non-integer inputs!

Step 13: At the End of the Program, Print the Count of Problems Out of N That the Player Got Right.

  • After all those nested statements, print the count of problems out of n that the player got right!

Step 14: Look Over Your Code!

  • Remember the importance of indentation in Python, as this program utilizes many nested statements.
  • Your final program should look like this.

Step 15: Run This Module and Enjoy Your Math Game!

  • After following these steps to code your math program, go ahead and hit Run Module.
  • Enjoy your simple addition game!