TA Evaluation! (at Midterm)

After the TA has left the room, please click on the link below to take an ANONYMOUS survey.

TA Midterm Survey

The TA will reenter in 5 minutes and class will resume.

# Conditionals Lab

James MacGlashan & Sue Evans

Hit the space bar for next slide

# Overview

Objectives for today's lab:
• Gain familiarity with working with conditionals
• Become familiar with using the mod operator
• Understand using conditionals to manage errors and special cases

# Step 0: Setup

The first step is to create a lab4 folder in your cs201/labs directory and make a lab4.py file to start programming.

```cd 201/labs
mkdir lab4
cd lab4
emacs lab4.py &
```

# Step 1 - What's a perfect number ?

Your task for lab 4 is to write a program that determines whether a number the user enters is a perfect number or not.

A perfect number is a positive integer that is equal to the sum of its divisors (excluding itself).
For example, the divisors of 6 are 1, 2 and 3. Since 6 = 1 + 2 + 3, 6 is a perfect number. The next three perfect numbers are 28, 496 and 8128.

6 = 1+2+3
28 = 1+2+4+7+14
496 = 1+2+4+8+16+31+62+124+248

30 != 1+2+3+5+6+10+15 Not a perfect number!! (42 in total!!)

# Step 2 - How do I do that ?

• Your task for lab4 is to write a program that takes a positive integer from the user as input and prints whether it is a perfect number or not.
• If the user enters a negative number, 0, or a floating point number, you should not continue and inform the user the number needs to be a positive integer. (Use the type function to help) You can end your program by making a call to sys.exit(). You'll need to import sys to be able to use sys.exit()
• You should loop through all integers less than the input integer, and add them together if they are divisors of the input integer.
• To loop through all integers less than the integer <number> you use the following format:
• ```for i in range(1, number):
#do something with i
```
• The modulus operator in python is the percent symbol '%'.
• The modulus of two numbers A % B is the remainder after dividing A by B.
• ```>>> 5 % 1
0
>>> 5 % 2
1
>>> 5 % 3
2
>>> 5 % 4
1
```
• This means that if B is a divisor of A then A % B is equal to 0.

# Bonus Step - Coodinates on a X/Y Plane

Use emacs to write bonus4.py that prompts the user to input x-y coordinates of a point in a Cartesian plane.

1. For the Lab 4 bonus, you will prompt a user for two values (x and y) to determine where that point lies either in a quadrant, origin, or on an axis. Use the chart above.
2. To start you will prompt the user for the following information
• x (x coordinate)
• y (y coordinate)
3. Based on the input information, the program should tell the user whether the point is located in:
• the origin
• x axis
• y axis