# Computer 12 Python Update

Today we finished our introduction to programming using Python.  Our main objective in this segment is to learn basic programming concepts, including output, input, looping, conditionals, variables, and functions.  The culmination of this segment of instruction was building a simple text-based calculator. Here is a sample of one of our calculators:

```#add function
def add(a,b):
result=a+b
return(result)
#subtract
def subtract(a,b):
result=a-b
return(result)
#multiply
def multiply(a,b):
result=a*b
return (result)
#divide
def divide(a,b):
result=a/b
return(result)
#Circumference
def Circumference(r):
circ=2*3.14159*r
return(circ)

#main function
def main():
#set selection = to 1 so the condition to start
#our while loop is true
selection=1
while selection>0:
#print the menu
print("1: Add")
print("2: Subtract")
print("3: Multiply")
print("4: Divide")
print("5: Circumference")
#get the user input
UserChoice=input("Please make a choice or 0 to exit: ")
#convert it to an integet
selection=int(UserChoice)
#if the choice is 0 we are finished!
if(selection==0):
print ("Goodbye")
elif selection>0 and selection<5: #an operation requiring 2 numbers
#Do Math!
#Get 2 Numbers and convert them to integers
num1=int(input("Enter the first number: "))
num2=int(input("Enter the second number: "))
#perform the desired operation
if selection==1:
x=add(num1,num2)
if selection==2:
x=subtract(num1,num2)
if selection==3:
x=multiply(num1,num2)
if selection==4:
x=divide(num1,num2)
#display the result
print("The result of your operation is: ",x)
elif selection==5: # we only need the radius to calculate the Circumference
num1=int(input("Enter the radius: "))
x=Circumference(num1)
print("The Circumference is: ",x)
elif selection>5: #any value over 5 is invalid
#invalid selection
print("Error!  Invalid Choice!")
#Tell the interpreter to start the program!
main()

```

mrcampbell / February 15, 2018 / Computer 12