解析CS10、Python编程讲解、辅导Python设计、辅导讲解Python、解析Python语言程序

- 首页 >> Python编程

CS10 Python Programming Homework 2

40 points

1. You must turn in your program listing and output for each program set. Start a new sheet or sheets of paper

for each program set. Each program set must have your student name, student ID, and program set

number/description. All the 4 program sets must be printed and submitted together with your Exam 2. Once

you complete your Exam 2 and leave the classroom you will not be able to submit Homework 2. Late

homework will not be accepted for whatever reasons you may have.

*********for this homework, you are also to submit Program set 4 to Canvas under Homework 2 link*********

a. Name your file : lastname_firstname.py

b. You still have to submit the paper copy together with the rest of the Homework 2.

c. You have till 11:59pm on the day of Exam 2 to submit Program Set 4 to Canvas. If the deadline

is past Program Set 4 will not be graded even if you submit the paper copy on time.

2. You must STAPLE (not stapled assignments will not be graded resulting in a zero score) your programming

assignment and collate them accordingly. Example Program set 1 listing and then output, followed by Program

Set 2 listing and output and so on.

3. Please format you output properly, for example all dollar amounts should be printed with 2 decimal places.

Make sure that your output values are correct (check the calculations).

4. Each student is expected to do their own work. IF IDENTICAL PROGRAMS ARE SUBMITTED, EACH

IDENTICAL PROGRAM WILL RECEIVE A SCORE OF ZERO.

Grading:

Each program set must run correctly both syntactically, logically, and display the correct output as specified. If

the program set does not run, a zero will be given. For each Program set, if the program executes properly with

proper syntax, logic, and displays the correct output, then points will be deducted for not having proper:

a. Comments 1 pt

- Your name, description at the beginning of each program set. Short description of the what

each section of your codes do.

b. Consistency/Readability 2 pts

- Spacing(separate each section of codes with a blank line

- Indentation

- Style (proper naming of variables no a,b,c – use descriptive and mnemonics)

c. Required elements 2 pts

- Use tools that have been covered in class

- proper formatting for output when specified

- all monetary values must be in 2 decimal places

d. Output (Each Program Set without an output will receive a zero)

- to be displayed at the end of the program listing(codes)

- must use test cases when provided

USE ONLY THE TOOLS YOU HAVE BEEN TAUGHT IN CLASS ONLY, IF YOU USE ANYTHING

WE HAVE NOT COVERED YET YOU WILL RECEIVE A ZERO FOR THAT PROGRAM SET .

Program Set 1(10 points)

Lottery program. The program randomly generates a two-digit number, prompts the user to enter a two-digit

number, and determines whether the user wins according to the following rules:

1. if the user’s input matches the lottery In the exact order, the award is $10,000.

2. if all the digits in the user’s input match all the digits in the lottery number, the award is $3,000.

3. if one digit in the user’s input matches a digit in the lottery number, the award is $1,000.

Program Set 2 Using Loops (Use while loop only for this program) (5 points)

Stock Transaction Program

Last month Joe purchased some stock from Schmoe, Inc.

Write a program that allows the user to input the followings as many times as he/she wants until the user is done

(example it can be 5):

1. The name of the stock

2. Stock Number of shares Joe bought

3. Stock purchase price

4. Stock selling price

5. Broker commission

displays the following paid for the stock(s) that Joe had transacted ( if Joe entered 5 sets of stocks transactions

then output 5 sets of stocks).

1. The Name of the Stock

2. The amount of money Joe paid for the stock (number of shares bought * purchase price)

3. The amount of commission Joe paid his broker when he bought the stock. (Amount he paid for stocks *

commission in percent)

4. The amount that Jim sold the stock for. (number of shares * selling price)

5. The amount of commission Joe paid his broker when he sold the stock. (Amount he sold shares *

commission in percent)

6. Display the amount of money Joe had left when he sold the stock and paid his broker (both times). If

this amount is positive, then Joe made a profit. If the amount is negative, then Joe lost money.

Profit/loss =(amount for sold stocks– commission) -(amount paid to buy stocks + commission)

Program Set 3 – Use for Loops only (5 Points)

Convert program 2 codes to use For loops to enter stocks data and output the stocks data.

You are to ask the user how many stocks does he/she wants to input. For example,

How many stocks do you want to enter now? 3

Your program must only allow user to enter 3 sets of stocks and output 3 sets of stocks.

Program Set 4 (20 Points)

This assignment will give you more experience on the use of:

1. integers (int)

2. floats (float)

3. conditionals

4. iteration

The goal of this project is to make a fictitious comparison of the new federal income tax based on the new tax bill

passed by the congress in December 2017 with the old federal income tax. You will ask the user to input their

taxable income. Use the income brackets given below to calculate the new and old income tax. For the sake of

simplicity of the project we will only consider individuals and not married users. We will also ignore any tax

deductions while calculating income tax—they can significantly alter the tax, but add too much complexity for our

programming project.

New income tax brackets (2018 and newer)

Rate Income range

10% Up to $9,525

12% $9,526 to $38,700

22% $38,701 to $82,500

24% $82,501 to $157,500

32% $157,501 to $200,000

35% $200,001 to $500,000

37% over $500,000

Old income tax brackets (2017 and older)

Rate Income range

10% Up to $9,325

15% $9,326 to $37,950

25% $37,951 to $91,900

28% $91,901 to $191,650

33% $191,651 to $416,700

35% $416,701 to $418,400

39.6% over $418,401

Assignment Background

Being in the 25% tax bracket doesn’t mean you pay 25% on everything you make. The progressive tax

system means that people with higher taxable incomes are subject to higher tax rates, and people with

lower taxable incomes are subject to lower tax rates.

For example, let’s say you’re a filer with $32,000 in taxable income. That puts you in the 15% tax

bracket in 2017. But do you pay 15% on all $32,000? No. Actually, you pay only 10% on the first

$9,325; you pay 15% on the rest. (Look at the tax brackets above to see the breakout.)

If you had $50,000 of taxable income, you’d pay 10% on that first $9,325 and 15% on the chunk of

income between $9,326 and $37,950. And then you’d pay 25% on the rest, because some of your

$50,000 of taxable income falls into the 25% tax bracket. The total bill would be $8,238.75 — about

16% of your taxable income, even though you’re in the 25% bracket.

Project Description

Your program must meet the following specifications:

1. At program start, prompt the user for their income

2. Repeatedly prompt the user for new income until a negative income is entered.

3. Calculate the income tax using the 2017 and 2018 tax bracket tables above.

4. For each income entered:

a. Calculate the 2017 income tax and store it in a variable.

b. Next calculate the 2018 income tax and store it in a variable

c. Print

i. The income

ii. The 2017 tax

iii. The 2018 tax

iv. The difference between the 2018 and 2017 tax rounded to cents.

v. The difference between the 2018 and 2017 tax as a percentage of the 2017 tax

rounded to cents

Assignment Deliverable

The deliverable for this assignment is the following file:

lastname_firstname.py – the source code for your Python program

Be sure to use the specified file name and to submit it for grading via Canvas before the project

deadline.

Assignment Notes

1. To clarify the project specifications, sample output is appended to the end of this document.

2. Use a while loop with a Boolean that keeps looping as long as the income is greater than or

equal to zero. Prompt for income before the loop and remember to convert the input string to

an int (so you are comparing an int in your Boolean expression). Remember to prompt again at

the end (aka “bottom”) of the loop.

4. There will be no error checking in this assignment. If a float or a string is entered at the

prompt, the program will crash.

Test Cases

Test Case 1

_____________________________________________________________

Enter income as an integer with no commas: 8000

Income: 8000

2017 tax: 800.0

2018 tax: 800.0

Difference: 0.0

Difference (percent): 0.0

Enter income as an integer with no commas: 15000

Income: 15000

2017 tax: 1783.75

2018 tax: 1609.5

Difference: -174.25

Difference (percent): 9.77

Enter income as an integer with no commas: 40000

Income: 40000

2017 tax: 5738.75

2018 tax: 4739.5

Difference: -999.25

Difference (percent): 17.41

Enter income as an integer with no commas: 100000

Income: 100000

2017 tax: 20981.75

2018 tax: 18289.5

Difference: -2692.25

Difference (percent): 12.83

Enter income as an integer with no commas: 200000

Income: 200000

2017 tax: 49399.25

2018 tax: 45689.5

Difference: -3709.75

Difference (percent): 7.51

Enter income as an integer with no commas: 500000

Income: 500000

2017 tax: 153818.85

2018 tax: 150689.5

Difference: -3129.35

Difference (percent): 2.03

Enter income as an integer with no commas: 1000000

Income: 1000000

2017 tax: 351818.85

2018 tax: 335689.5

Difference: -16129.35

Difference (percent): 4.58

Enter income as an integer with no commas: 10000000

Income: 10000000

2017 tax: 3915818.85

2018 tax: 3665689.5

Difference: -250129.35

Difference (percent): 6.39

Enter income as an integer with no commas: -1

Test 2

______________________________________________________________

Enter income as an integer with no commas: -1


站长地图