# CS21 Lab 3: Conditionals and Booleans

Due Saturday, Feb 15th 11:59pm

This lab assignment requires you to write three programs in python. First, run update21. This will create the cs21/labs/03 directory and copy over any starting-point files for your programs. Next, move into your cs21/labs/03 directory and begin working on the python programs for this lab. The pwd command helps you verify that you are in the correct sub-directory.

```\$ update21
\$ cd cs21/labs/03
\$ pwd
/home/your_user_name/cs21/labs/03
```
We will only grade files submitted by handin21 in this labs directory, so make sure your programs are in this directory!

In this exercise you will implement an extremely simple version of college admissions logic. You should write your solution in a file called admissions.py. Here is the logic used by our fictitious college:

1. If the applicant is younger than 13, do not admit.
2. Otherwise, if the applicant scored above 1500 on the SAT, admit.
3. Otherwise, if the applicant has a high school GPA above 3.0 and has at least one extracurricular activity, admit.

Here are some examples of how your program should behave:

```\$ python admissions.py
Age? 17
SAT score? 1520
High school GPA? 2.8
Number of extracurriculars? 3

```
```\$ python admissions.py
Age? 10
SAT score? 1580
High school GPA? 3.8
Number of extracurriculars? 1

```
```\$ python admissions.py
Age? 19
SAT score? 1330
High school GPA? 3.3
Number of extracurriculars? 7

```

Pay attention to the formatting of our sample output and do your best to make your program's output match ours.

2. Finding text

In this exercise you will count the number of times a short string appears in a longer one. Write your program in a file called find.py. Your program should produce the following sort of interaction with the user:

```\$ python find.py
Haystack: Hello Goodbye
Needle: Good
'Good' appears in your haystack 1 time.
```
```\$ python find.py
Haystack: omnomnom
Needle: mnom
'mnom' appears in your haystack 2 times.
```
```\$ python find.py
Haystack: Little Miss Muffet
Needle: tuffet
'tuffet' appears in your haystack 0 times.
```

The Python standard library has find and index operations on strings. Do not use these for this exercise. Only use things we have covered in class: variables, loops, conditionals. Note: capitalization matters; "HELLO" is not the same as "hello". This should be slightly easier than making your program case-insensitive.

Pay attention to the formatting of our sample output and do your best to make your program's output match ours.

3. Tax brackets

In this exercise you will write a program, in a file named tax_float.py, that asks the users for their annual income and outputs the amount of federal income tax they have to pay.

America's federal income tax system uses an idea called brackets. If your income is high enough, it is divided up into pieces that are taxed at different rates. Your total income tax bill is the sum of all the pieces. Here is the current federal income tax schedule for individuals (this is what you should use in your program):

 Rate More than Up to 10% 0 9,075 15% 9,075 36,900 25% 36,900 89,350 28% 89,350 186,350 33% 186,350 ∞

(We left out a few brackets to save you a little typing.)

Here's an example to help you understand how brackets work. If Sam earns \$90,000, the "first" 9,075 is taxed at 10%, the 9,075-36,900 range is taxed at 15%, the 36,900-89,350 range is taxed at 25% and the 89,350-90,000 range is taxed at 28%. Therefore, Sam's total federal income tax bill for the year is:

(9075)*0.10 + (36900-9075)*0.15 + (89350-36900)*0.25 + (90000-89350)*0.28

If you plug this all into a calculator, you will get \$18,375.75, which means that Sam's effective income tax rate is approximately 20.4% (\$18,375.75 / \$90,000)

For tax_float.py you should use floats to store dollar amounts. As you will see in the hacker challenge, this is the simpler way to go. Here are two examples of the tax_float.py program:

```\$ python tax_float.py
Enter your annual income (in dollars): 90000
You owe the federal government \$18375.75 in income tax.
Your effective tax rate is 20.4%.

\$ python tax_float.py
Enter your annual income (in dollars): 61234.56
You owe the federal government \$11164.89 in income tax.
Your effective tax rate is 18.2%.
```

Note: To get a literal % character in a %-formatting string, you have to write %%.

Hacker's challenge 1. Down with floating point

As always, this is an optional bonus exercise. It is challenging and is not required, and you should not attempt it until you are completely finished with the other lab exercises. It will also not get you any extra points -- you will only be rewarded with extra knowledge and satisfaction. :)

Real financial software never uses floating point numbers to represent quantities of currency. As mentioned in class, floats have some issues related to limited precision. If you do a long sequence of operations with floats it is possible for small precision errors to accumulate into a macroscopically incorrect result. People get grouchy when money disappears because of an obscure numerical error in a computer.

When floating point is unacceptable we have to use integers to represent numbers. But how can we handle fractions of a dollar (i.e. cents) with ints? We use the fixed-point trick! We change the units that we use. For example \$1.42 is equal to 142¢. We need a float to represent 1.42, but we can use an int to represent 142.

Write a new program tax_int.py that has exactly the same user interface as tax_float.py, but does all internal arithmetic with ints instead of floats. The tricky parts are:

• The user enters their income in dollars, and they may type a fractional number. You have to convert this to an integral number of cents.
• All the internal calculations must be done on ints. Hint: notice that multiplying by .75 is mathematically the same as multiplying by 75 and then dividing by 100.
• Printing the final answer is a little trickier, because it is stored in cents, but we want it to be printed in dollars.

Hacker's Challenge 2. My own special brackets

As always, this is an optional bonus exercise. It is challenging and is not required, and you should not attempt it until you are completely finished with the other lab exercises. It will also not get you any extra points -- you will only be rewarded with extra knowledge and satisfaction. :)

Write a new program tax_brackets.py that is similar to tax_float.py, but allows the user to make their own brackets. Here is an example of how it should work:

```\$ python tax_float.py
How many brackets would you like? 3
Rate for bracket 1: 0.20
Upper limit for bracket 1: 10000
Rate for bracket 2: 0.10
Upper limit for bracket 2: 100000
Rate for bracket 3: 0.30

Enter your annual income (in dollars): 1000000
You owe the federal government \$281000.00 in income tax.
Your effective tax rate is 28.1%.
```

Notice that the last bracket never has an upper limit; that rate applies to all income over the previous bracket's limit. You are free to assume that the upper limits increase as you go from lower to higher brackets.

Submit

Once you are satisfied with your programs, hand them in by typing handin21 at the Linux prompt.

You may run handin21 as many times as you like. We will grade the most recent submission submitted prior to the deadline

Remember: for this lab, programs must appear in your cs21/labs/03 directory. If you create your programs in a different directory, use the unix mv or cp commands to move or copy them into the cs21/labs/03 directory. For example:

` mv  myprog.py  ~/cs21/labs/03/myprog.py`