TA Evaluation! (at Midterm)
Please make sure your TA places their name on the board.
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.
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.
From your home directory :
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 ?
>>> 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.
- 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.
- To start you will prompt the user for the following information
- x (x coordinate)
- y (y coordinate)
- Based on the input information, the program should tell the user
whether the point is located in:
- the origin
- x axis
- y axis
- Quadrant 1
- Quadrant 2
- Quadrant 3
- Quadrant 4