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.
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 - Eligibility
Use emacs to write bonus4.py that performs the following eligibility
tests and prints out a report that states what things the user is
eligible for and if they're not eligible for something, why not.
- For the Lab 4 bonus, you will prompt a user for information and tell
them how eligible they are to smoke, drink alcohol, rent a car in
Maryland, vote in Maryland, be a US senator, and be President of the
United States
- To start you will prompt the user for the following information
- Age
- Length of time as a US citizen
- Whether the user was born in the US
- Length of time living in the US
- How many times the user has been convicted a felony
- Based on the input information, the program should tell the user
whether they are eligible for each of the criteria, and if not, why
not.
- To be able to smoke tobacco, the user must be >=18 years old.
- To be able to drink alcohol, the user must be >=21 years old.
- To be able to rent a car in Maryland, the user must be >=25 years
old.
- To be able to vote in Maryland, the user must be >= 18 years old,
and have not been convicted of a felony more than once.
- To be a US senator the user must be >= 30 years old, and have been
a citizen for at least 9 years.
- To be the President of the United States the user must be >=35
years old, was born in the US, and have lived in the US for at
least 14 years.
Challenge Step - Security Clearance
Make a copy of your bonus4.py file called challenge4.py and modify the
new file to include the eligibility for security clearance.
- Add the following questions for the user to answer.
- Whether the user is dating or is married to someone who is a
foreign national
- Whether the user has much financial debt (standard things like
student loans may be ignored)
- Whether the user has any DUIs
- Whether the user has used illegal drugs, and if so how many years
ago (0 if less than a year)
- For security clearance, exceptions can always be made, so the
following is a point-based heursitic for how difficult it would be
to obtain security clearance.
- The point system for security clearance is as follows :
- Lack of citizenship awards 5 points.
- Not being born in the US awards 2 points.
- Dating or being Married to a foreign national awards 2 points.
- Drugs : A user receives :
4 points for using illegal drugs less than a year ago
2 points if greater than one, but less than two years ago
1 point for greater than two, but less than three years ago
no points for greater than three years ago
- A user gets 6 points if they've been convicted of any felonies
- A user receives 3 points if they have any DUIs
- A user receives 6 points if they are not at least 18 years old
- If the user has 0 points, getting security clearance should be trivial
- If the user has 1-3 points the user may have some difficulty getting
security clearance
- If the user had 4-5 points, it will be very hard for the user to get
security clearance
- If the user has >=6 points it will be next to impossible for the user
to get security clearance