DEV Community

Cover image for Python - Calculator sample
Flavio Campelo
Flavio Campelo

Posted on • Edited on

2 2

Python - Calculator sample

📮 Contact 🇧🇷 🇺🇸 🇫🇷

Twitter
LinkedIn


Calculator sample

Create a new file called Calculator.py and add this code

firstNumber = input("Enter the first number: ") # it will wait user input for the first number
secondNumber = input("Enter the second number: ")  # it will wait user input for the second number
total = firstNumber + secondNumber # it will keep the result in a variable called total
print(total)  # it will show the result
Enter fullscreen mode Exit fullscreen mode

To execute this python program you can execute it with the command

python3 calculator.py
Enter fullscreen mode Exit fullscreen mode

Image 1

Here we have a problem. 2 + 3 is 5, so why the output is showing 23? It happens because anything received by the input il will be always a string type. It's the same as writing this

firstNumber = "2"

instead of this

firstNumber = 2

So, we have to convert the received input to a number.

firstNumber = int(input("Enter the first number: ")) # it will wait user input for the first number
secondNumber = int(input("Enter the second number: "))  # it will wait user input for the second number
total = firstNumber + secondNumber # it will keep the result in a variable called total
print(total)  # it will show the result
Enter fullscreen mode Exit fullscreen mode

And then, we have the correct answer:

Image 2

What about improving the answer message? We would like to show a message like The result is: beside of the result. So, here is our new code:

firstNumber = int(input("Enter the first number: ")) # it will wait user input for the first number
secondNumber = int(input("Enter the second number: "))  # it will wait user input for the second number
total = firstNumber + secondNumber # it will summarize
print("The result is: " + total)  # it will show the result
Enter fullscreen mode Exit fullscreen mode

If we try to execute it, we get an error TypeError: can only concatenate str (not "int") to str.

Image 3

It happens because python can't concatenate string and integer types directly. So, we have to convert the int variable to string before show the message. Our new code will look like that:

firstNumber = int(input("Enter the first number: ")) # it will wait user input for the first number
secondNumber = int(input("Enter the second number: "))  # it will wait user input for the second number
total = firstNumber + secondNumber # it will summarize
print("The result is: " + str(total))  # it will show the result
Enter fullscreen mode Exit fullscreen mode

And everything is correct now.

Image 4

Notes

You can access this code on github.

Typos or suggestions?

If you've found a typo, a sentence that could be improved or anything else that should be updated on this blog post, you can access it through a git repository and make a pull request. If you feel comfortable with github, instead of posting a comment, please go directly to https://github.com/campelo/documentation and open a new pull request with your changes.

👋 Kindness is contagious

Please leave your appreciation by commenting on this post!

It takes one minute and is worth it for your career.

Get started

Thank you! 🙏

Top comments (0)

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay