Python exercises for beginners (2)

Python exercises for beginners (2)

(Financial Application: Compound Interest Value) Suppose you deposit $100 per month into a savings account that earns 5% annual interest. Therefore, the monthly interest rate is 0.05/12=0.004 17. After the first month, the amount in the account becomes:

100 * (1 + 0.00417) = 100.417
After the second month, the number in the account becomes:
(100 + 100.417) * (1 + 0.00417) = 201.252
After the third month, the number in the account becomes:
(100 + 201.252) * (1 + 0.00417) = 302.507
and so on.
(Financial Application: Compound Interest Value) Suppose you deposit $100 per month into a savings account that earns 5% annual interest. Therefore, the monthly interest
rate is 0.05/12=0.004 17. After the first month, the number in the account becomes:
100 * (1 + 0.00417) = 100.417
After the second month, the number in the account becomes:
(100 + 100.417) * (1 + 0.00417) = 201.252
The third Months later, the number in the account becomes:
(100 + 201.252) * (1 + 0.00417) = 302.507
and so on.

code show as below:

a = float(input("请输入你想要存的本金:"))
h = a * (1 + 0.00417)
c = (h + a) * (1 + 0.00417)
d = (c + a) * (1 + 0.00417)
e = (d + a)* (1 + 0.00417)
f = (e + a) * (1 + 0.00417)
g = (f + a) * (1 + 0.00417)
print("%s的本金得到利息后账户余额为:%s"%(a,g))



(Financial Application: Calculate Future Investment Amount) Write a program that reads the investment amount, annual interest rate, and number of years and then displays the future investment amount using the following formula:


Future investment amount = investment amount

code show as below:

money = float(input("请输入你想存放的本金:"))
mouth = 0.0425 / 12
sume = money * (1 +mouth)**12
print("%s的本金存放一年后的总额为:%s"%(money,sume))



(Reverse Numbers) Write a program that prompts the user to enter a four-digit integer and then displays the number with the digits reversed. Below is an example run.

Enter an integer: 3125
The reversed number is 5213

code show as below:

a = int(input("请输入一个你想要颠倒的四位整数:"))
b = (a%10 )
c = (a//10%10)
d = (a//100%10 )
e = (a//1000%10 )
print("反向顺序后结果为%s%s%s%s"%(b,c,d,e))



Now, let's look at an example program that uses the features discussed in this section. Suppose you want to develop a program that sorts a certain amount of money into smaller monetary units. The program lets the user enter a total amount, which is a floating-point value expressed in dollars and cents, and then outputs a report listing the currency equivalents: dollars, quarters, dimes, nickels, and cents. Divide into numbers, as shown in the example run. Your program should report the maximum number of dollars, followed by the number of quarters, dimes, nickels, and cents, to get the minimum number of coins.

Here are the steps to write this program:

1) Prompt the user to enter a decimal number with a decimal point, for example: 11.56.
2) Convert the amount of money (11.56) into a fraction (1156).
3) Divide the fraction by 100 to get the dollar amount. Use the fraction %100 to get the remainder, which is the remaining number.
4) Divide the remaining fraction by 25 to get the number of quarters. Use the fraction %25 to get the remainder, which is the remaining fraction.
5) Divide the remaining fraction by 10 to get the number of dimes. Use the fraction %10 to get the remainder, which is the remaining fraction.
6) Divide the remaining fraction by 5 to get the number of nickels. Use the fraction %5 to get the remainder, which is the remaining fraction.
7) The remaining fraction is the number of pennies.
8) Display the results.

code show as below:

a = float(input("请输入一个十进制的数字:"))
b = a * 100
c = b / 100 
d = b % 100 / 25
e = d % 25 / 10
f = e % 10 /5
g = f % 5
print(b)
print(c)
print(d)
print(e)
print(f)
print(g)



(Financial Application: Payroll) Write a program that reads the following information and then prints a payroll report.

Employee name (Example: Smith)
Hours worked per week (Example: 10)
Hourly compensation (Example: 9.75)
Federal withholding tax rate (Example: 20%)
State withholding tax rate (Example: 9%)
An example run is shown below.
Enter employee's name: Smith
Enter number of hours worked in a week: 10 . Enter
Enter hourly pay rate: 9.75 Enter
Enter federa1 tax wi thholding rate: 0.20 Enter
Enter state tax wi thholding rate: 0.09 Enter
Emp1oyee Name: Smi th
(Financial Application Program: Payroll) Write a program that reads the following information and then prints a salary report.
Employee name (Example: Smith)
Hours worked per week (Example: 10)
Hourly compensation (Example: 9.75)
Federal withholding tax rate (Example: 20%)
State withholding tax rate (Example: 9%)
An example run is shown below.
Enter employee's name: Smith
Enter number of hours worked in a week: 10 .
Enter hourly pay rate: 9.75
Enter federa1 tax wi thholding rate: 0.20
Enter state tax wi thholding rate: 0.09
Emp1oyee Name: Smith
Hours Worked: 10.0
Pay Rate: $9.75
Gross Pay: $97.5
Deductions:
Federal Wi thholding (20.0%): $19.5
State Withholding (9.0%): $8.77
Tota1 Deduction: $28.27
Net Pay: $69.22

code show as below:

name = input('请输入雇员姓名:')
time = int(input("请输入一周工作时间:  "))
val = float(input("请输入每小时报酬:  "))
Lrate = float(input("请输入联邦预扣税率:  "))
Zrate = float(input("请输入州预扣税率:  "))
money1 = time * val * Lrate
money2 = time * val * Zrate
total = money1 + money2
totalval = time * val - total
print("雇员姓名:%s"%(name))
print("一周工作时间: %sh"%(time))
print("每小时的报酬:$%s"%(val))
print("联邦预扣税:$%s"%(money1))
print("州预扣税:$%s"%(money2))
print("总共扣税:$%s"%(total))
print("收入:$%s"%(totalval))



Until it is perfected,

Guess you like

Origin blog.csdn.net/Zombie_QP/article/details/123871491