DEV Community

Cover image for Python - Calculator sample
Flavio Campelo
Flavio Campelo

Posted on • Edited on

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.

Top comments (0)