Skip to content
Sign upLog in
← Back to Community

how to make a calculator in python

Profile icon
gatorade322

This is going to be a tutorial for beginners who know the basics of python. That is floats, and input and print. So, were going to start with two lines of code:

import math while True: math = input()

This will get the computer the information for it to know if we are going to do multiplication, division, addition, or subtraction. We will start with addition:

if math == “plus”: digit_1 = input() digit_2 = input()

This will get us all the inputs we need. If you want, you can add a + or plus in there, but I just just put nothing. This is what it would look like if there was a plus:

if math == “plus”: digit_1 = input() digit_2 = input(“plus or +”)

Next we need to add it together. We can’t do answer = digit_1 + digit_2, or that would be monkey math (which would be funny, but we don’t want it in our calculator). You can do that if you want. So we will just add float(digit_1) + float(digit_2) instead:

answer = float(digit_1) + float(digit_2) print(answer)

The rest of the code is basically the same, but with -, *, and /. So just copy and paste this all in to your repl:

elif math == "minus": a = input() b = input() c = float(a) - float(b) print(c) elif d == "square": a = input() b = float(a) * float(a) print(b) elif math == "divide": a = input() b = input() if b == "0": print("infinity") else: c = float(a) / float(b) print(c) elif math == "times": a = input() b = input() c = float(a) * float(b) print(c)

It’s pretty simple. I’ll just explain the infinity filter. If you divide anything by zero it’s infinity, or if you use a calculator, it will say undefined, but if you think about it, there are infinate zeroes. If you don’t add the filter, there will be a divide by zero error. So we just check if it’s zero, and if it is, it will print infinity. Also simple!

I hope you liked this tutorial. It was really fun to make. I also hope this influences future coding, and I hope you have fun coding!
Goodbye.

Voters
Profile icon
LeoSiemon
Profile icon
YAMENMOHAMED
Profile icon
3mk-6FSH-6FSH
Profile icon
FabionPyle
Profile icon
ycasher
Profile icon
AntoninaV1
Profile icon
Ransherkaler
Profile icon
AlfredSham
Profile icon
dtnuthalapati
Profile icon
ddddffdsd45
Comments
hotnewtop
Profile icon
XanderEhlert

you can do this in one line:

print('Your calculation comes to {0}.'.format(eval(input('What do you want to calculate?'))))
Profile icon
gatorade322

I'll try that. I might not be able to post the new version though. Upvote for the seggustion! :D

Profile icon
XanderEhlert

@gatorade322
good tutorial, ill give you an upvote

one more thing: you never say what d is

Profile icon
gatorade322

you're right. That's an error. At the top, just assume math is d. Thanks for the upvote though!

Profile icon
ANoobpro

it says line 4 invalid syntax

Profile icon
Denalgus2

@ANoobpro
yeah same with me

Profile icon
Denalgus2

@ANoobpro
yeah

Profile icon
gatorade322

@Denalgus2
I fixed it!

Profile icon
Codemonkey51

Cool! This is a great tutorial for anyone who wants to make a calculator

Profile icon
gatorade322

Thanks!

Profile icon
Codemonkey51

Your welcome

@gatorade322

Profile icon
gatorade322

I just posted part one of 2D minecraft. You should check it out. It should be one of the first things in the learn section

Profile icon
Codemonkey51

Ok cool I'll check it out

@gatorade322

Profile icon
gatorade322

@Codemonkey51
OK

Profile icon
NatashaJeter

doesnt work

Profile icon
tribune

this is actually very simple 100 percent

Profile icon
gatorade322

@tribune
Thanks!

Profile icon
PranavrajSK

It is saying error in the 6th line of code

Profile icon
mannifn705

Not working

Profile icon
[deleted]

[ link redacted by moderators for advertising ]

Profile icon
[deleted]

why does it only add?

Profile icon
XanthusPettitt

thanks for this!

Profile icon
Denalgus2

guys, can someone help? It says that line 4 is invalid. I have tried everthing but It doesnt help

Profile icon
gatorade322

@Denalgus2
I made a typo in this tutorial, on line four instead of 'd' add 'math', sorry about that!

Profile icon
neimat

For some reason my calculator is only adding? I'm not sure where the error is.

https://repl.it/join/ncnmyjmg-neimat

Profile icon
gatorade322

@neimat
I don't know, did you use all the code?

Profile icon
gatorade322

@neimat
, it doesn't look like you put the variables 'a' and 'b' in the adding section. This may be the problem.

Profile icon
gatorade322

@neimat
, try switching the code to python3

Profile icon
patricksutherla

no me sirve me manda codigo de error

Profile icon
gatorade322

@patricksutherla
are you using python turtle

Profile icon
gatorade322

@patricksutherla
I can't understand that

Profile icon
gatorade322

@patricksutherla
Intente cambiar la "d" en la línea cuatro a matemáticas, cometí un error tipográfico. ¡Lo siento por eso!

Profile icon
[deleted]

it always does [email protected]

Profile icon
20Chown

@gatorade322
I think he/she is saying that on line 4, there is a mistake/error, that only lets it do maths????

Profile icon
20Chown

Not sure but probs something like that

Profile icon
20Chown

oops typo