Skip to content

Instantly share code, notes, and snippets.

@FlashNoob98
Forked from santoshmn26/turtle_calculator.py
Created November 14, 2017 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlashNoob98/8608e4287ccde0e539022892f6324450 to your computer and use it in GitHub Desktop.
Save FlashNoob98/8608e4287ccde0e539022892f6324450 to your computer and use it in GitHub Desktop.
Two simple calculator programs
# 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
# Let's write a calculator program that has the computer ask the user for two number to calculate, then return the answer
# to have a program accept input from a user, we use the raw_input() function, Python automatically considers what the user types in as a string.
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
print(answer)
####
####
# To make this a more interesting program, let's have Python "draw" out the answer!
# To do this, we import the module, turtle
# a module is just a program already written. We can use functions inside that program in our own, once we import it
import turtle
t = turtle.Turtle()
print("Please give me a number")
number1 = raw_input()
print("Please give me another number")
number2 = raw_input()
number1 = int(number1)
number2 = int(number2)
print("What operation would you like me to do with the two numbers?")
operation = raw_input()
if operation == "+":
answer = number1 + number2
elif operation == "-":
answer = number1 - number2
elif operation == "*":
answer = number1 - number2
elif operation == "/":
answer = number1 / number2
# To have 'turtle' draw out the answer, we just use a couple functions from the turtle program:
t.write(answer, font = (None, 40, "bold"))
turtle.done()
# Once we run this program, turtle will draw out whatever the answer is in a separate window!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment