Skip to content

Instantly share code, notes, and snippets.

# joetechem/turtle_calculator.py Created Feb 24, 2017

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!

# same code in python 3.6

import turtle
def drawans(answer): # a function to handle the drawing
t=turtle.Turtle()
t.write(answer,font=(None,40,"bold")) # drawing the answer using the turtle
turtle.done()
x =int(input('Enter a number')) #input for the first number
y= int(input('Enter the second number')) #input for the second number
operation=input("Enter the operation") #input for the operation symbol (+,-,/ or )
if(operation=='+'): #if and else conditions for various symbols
answer=x+y
print(answer)
drawans(answer)
exit() # exit once the operation is completed instead of checking for other operations
elif(operation=='-'):
answer=x+y
print(answer)
drawans(answer)
exit()
elif(operation=='
'):
answer=x+y
print(answer)
drawans(answer)
exit()
elif(operation=='/'):
answer=x+y
print(answer)
drawans(answer)
exit()

to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.