Lab 2-1: Single Digit Calculator

Lab Requirements and Specifications

In this lab, you are going to be creating a simple calculator that will only operate on single-digit numbers. Your program should use the input() function to allow the user to enter the input. The output should be something like "The result is ____"

The input will always be three characters long. The first character will always be the first (single-digit) number, the third character will always be the second (single-digit) number, and the second character will always be the operator.

Write your program so that it can handle the following operations:
  • Addition +
  • Subtraction -
  • Multiplication *
  • Division /
  • Modulo %

As an example, if given an input of "3*5", your program should perform the calculation and print out "The result is 15". The result may have more than one digit. The result may also be negative, but only if you subtract one number from a number that is smaller. Your program does not need to accept negative numbers as inputs.

Again, please remember that input() will always give you a string. You will have to use conversion functions such as int() or float() in order to use a string (or its parts) as a number.

Your program will break the input into three parts - the first term, the operator, and the second term. Since your input is a string, you can use string indexing to grab specific parts. For example, "Howdy!"[3] will give you "d" since the index starts at zero. Feel free to look back on previous pages for assistance. Once you have your parts broken up (and preferably saved as variables), how do you know what your program will do? Which part of the input expression determines what the program does? You will want to use conditionals to control what the program will do and when it will do it.

You should name your file FILN_simplecalc.py, where FILN is first initial and last name, all lowercase, no space.

Testing Your Program

You can test your program by feeding it the following inputs and comparing your output to the result.

Input Output
4+9 The result is 13
4-6 The result is -2
8*5 The result is 40
9/6 The result is 1.5
8%3 The result is 2

The following space is provided in case you want to test code out or write it in the browser:

Taking it Further

We can expand the capabilities of our calculator by allowing it to handle lengths of multiple digits!

We will do this by asking the user for three things:
  • The expression in the format term1 operator term2, no spaces, and the terms can be any length. Example: 492-51
  • The length of the first term in terms of digits. From the example above, that would be 3
  • The length of the second term in terms of digits. From the example above, that would be 2.

Using that information, you will do something similar to your original program, but this time, you will have to use string slicing. As a reminder, string slicing is similar to string indexing, but you provide a range of indexes instead of a single index. Example: "492-52"[0:3] will be "492" because the slice expression [0:3] means that we want the everything from the 0th to 2nd index inclusive. Please note that though the length of "492" is 3, because strings use zero-based indexing, we can’t simply use the length of the string to determine the second number in the slice. You will need to do a little arithmetic to determine the start and end indexes of each number in the input expression.

Next Section - Lab 2-2: Pluralizer