Contents

# Welcome to another tutorial, today we will be making a command line calculator.

To start let’s make the menu, for this I usually list off a few options and then add an input like so

print(“ Subtraction”)
print(“ Multiplication”)
print(“ Division”)
option = input(“Enter one of the options [1-4]: “)

Once you have that in your code we should start working on the actual calculator, for this you will learn how to convert strings to floats. To show this have a look at the addition code

if option == ‘1’:
num1 = input(“Enter the first number you wish to add: “)
num2 = input(“Enter the second number you wish to add: “)

as you can see the strings num1 and num1 are attached to input methods, when you see the string called “add” it has both num1 and num2 in it except it is surrounded by a parenthesis with “float” in front of it. Basically, float makes the string into a decimal so if you input 8.4 it will output 8.4, this is different from int() as int() only outputs whole numbers.

if option == ‘2’:
num1 = input(“Enter the first number you wish to subrtract from: “)
num2 = input(“Enter the second number you wish to subtract: “)
sub = float(num1) – float(num2)
print(sub)

if option == ‘3’:
num1 = input(“Enter the first number you wish to multiply: “)
num2 = input(“Enter the second number you wish to muliply by: “)
mult = float(num1) * float(num2)
print(mult)

if option == ‘4’:
num1 = input(“Enter the first number you wish to divide: “)
num2 = input(“Enter the second number you wish to divide by: “)
div = float(num1) / float(num2)
print(div)

This code is all pretty much the same except for the input message and the math behind it. If you have any questions feel free to leave a comment or ask on the discord.