Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
representing a basic calculator using python
A calculator program that tells the computer to ask a user for two numbers to calculate, then returns the answer
from the user's input. Python automatically considers what the user types in as a string.
This text and the text above is surrounded by 3 double quotes, which makes it a "docstring".
Docstrings are one way to convey what is happening in a program, or what is supposed to happen, they do not have a program "do" anything.
# Any line that starts with a pound sign (#) is known as a comment, Python knows to ignore comments.
# Comments are useful for explaining what you are doing in a program to yourself or someone else who might
# be working on the same program.
print("Please give me a number")
number1 = raw_input() # here's our first variable
print("Please give me another number")
number2 = raw_input() # here's our second variable
# We need to convert the value of the variables to a whole number, so Python can calculate the numbers
number1 = int(number1)
number2 = int(number2)
print("What operation would you like me to do with the two numbers?")
operation = raw_input()
# Below is our condition statements, or IF and THEN statements
# notice the indentations, these are called blocks of code
# anything that is indented under a statement, is part of that first statement
# checking if the input from the user is addition
if operation == "+":
answer = number1 + number2
# checking if the input from the user is subtraction
elif operation == "-":
answer = number1 - number2
# checking if the input from the user is multiplication
elif operation == "*":
answer = number1 - number2
# checking if the input from the user is division
elif operation == "/":
answer = number1 / number2
# Now, to print the answer
### NOTE:
# For the first part of the program, we could shorten the amount we type by combining lines of code.
# For instance, lines 13 - 21 can be rewritten as:
# number1 = int(raw_input("Please give me a number"))
# number2 = int(raw_input("Please give me another number"))
# We'd get the program to do the same thing, only with fewer lines of code.
# Remember, engineers are concerned with building things efficiently!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment