How to Make a Simple Calculator Using Python

How to Make a Simple Calculator Using Python


Published at - Dec 08, 2021

Here’s a small project for you if you’re new to Python. To develop your abilities, make a very basic calculator! I’ll teach you how to create one in this article.

Creating Inputs

We’ll need to make three inputs for the user to fill up with their chosen values.

We begin by creating num1, which is the first number the user must compute.

num1 = int(input("❯ Enter The First Number: "))

Second, we design an operator that the user may use to input data. If the user needs to multiply, for example, they may just type *.

operator = (input("❯ Enter '+', '-', '*' or '/'"))

Finally, we generate num2, which is the second number the user must compute.

num2 = int(input("❯ Enter The Second Number: "))

Remember to include int(integer) before the inputs!

Logic Writing

Now we’ll write the code for the calculator’s logic.

if Statements

We’ll use ifstatements to write code that adds, subtracts, multiplies, or divides using the calculator.

Addition

if operator == '+':
    answer = num1 + num2

Subtraction

if operator == '-':
    answer = num1 - num2

Multiplication

if operator == '*':
    answer = num1 * num2

Division

if operator == '/':
    answer = num1 / num2

That concludes the logic!

The Last Step

The only thing left to do is print it!

print("❯ The Answer Is " + str(answer))

Make sure str(string) is present once again!

That’s all there is to it; the calculator is now complete!

Congratulations on this project, and good luck with your studies!

Here’s how it turned out:

[Source](https://res.cloudinary.com/practicaldev/image/fetch/s--tmyuJWbG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/enm23bjjx7zejw02elz3.png)Source

I hope you enjoyed reading this lesson; you can follow me to read more tutorials from me in the future. Thank you for your time.

By becoming a Medium member, you can support me and your other favorite authors. Thanks! 👇 Join Medium with my referral link - Harendra Verma As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…medium.com

More content at plainenglish.io. Sign up for our free weekly newsletter here.





Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile