Lab 2

Description

This lab shows how to use the if statement and the while loop. It asks the user to enter two numbers and to choose a selection and the program will run based on the selection the user chose. After it runs, if the user didn’t use the number 0, the program will ask for two more numbers and run again till the user decides to put a 0 in one of the number choices.

Code

# Alex Charles
# Lab 2 – Control Flow
# February 28, 2013
# Section 9306
x = input(‘Enter the 1st number: ‘)
y = input(‘Enter the 2nd number: ‘)
while x!=0 and y!=0:

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’

selection = input(‘Enter a selction: ‘)
if selection == 1:
result = x+y
print ‘The sum of x and y is’, result
elif selection == 2:
result = x-y
print ‘The difference of x and y is’, result
elif selection == 3:
result = x*y
print ‘The product of x and y is’, result
elif selection == 4:
result = x/y
print ‘The quotient of x and y is’, result
elif selection == 5:
result = x%y
print ‘The remainder of x and y is’, result
else:
if x<y:
print ‘The number’, x, ‘is less than’, y
elif x>y:
print ‘The number’, x, ‘is greater than’, y
else:
print ‘The number’, x, ‘is equal to’, y

x = input(‘Enter the 1st number: ‘)
y = input(‘Enter the 2nd number: ‘)

Screenshot