Lab 3

Description

This lab uses the previous lab I posted earlier and uses functions to complete the selections asked to complete. This time the user is asked to choose a selection before choosing two integers to complete the program. Also different with this program is instead of killing the program by making one of the integers 0, there is an extra selection provided that exits the program for the user.

Code

# Alex Charles
# 3/7/13
# Session 9306
# Lab 3 – Functions

selection = input(‘Enter a selction: ‘)
while selection != 7:

x = input(‘Enter the 1st number: ‘)
y = input(‘Enter the 2nd number: ‘)
print ‘1 – The sum x + y’
print ‘2 – The difference x – y’
print ‘3 – The product x * y’
print ‘4 – The quotient x/y’
print ‘5 – The remainder x%y’
print ‘6 – Whether x is less than, equal, or greater than y’
print ‘7 – Exit program’

if selection == 1:
def addition (x,y):
sum = x+y
return sum
print x, ‘plus’, y, ‘equals’, addition
elif selection == 2:
def subtraction (x,y):
difference = x-y
return difference
print x, ‘minus’, y, ‘equals’, subtraction
elif selection == 3:
def multiplication (x,y):
product = x*y
return product
print x, ‘times’, y, ‘equals’, multiplication
elif selection == 4:
def division (x,y):
quotient = x/y
return quotient
print x, ‘divided by’, y, ‘equals’, division
elif selection == 5:
def leftover (x,y):
remainder = x%y
return remainder
print x, ‘divided by’, y, ‘has a remainder of’, leftover
elif selection == 6:
if x<y:
def less_than (x,y):
less = xy:
def greater_than (x,y):
greater = x>y
return greater
print x, ‘is greater than’, y
else:
def equal_to (x,y):
equal = x==y
return equal
print x, ‘is equal to’, y
else:
print ‘Wrong selection.’

selection = input(‘Enter a selction: ‘)

Screenshot