第三章 数字函数、字符串和对象(缺最后几道)

Python几何与金融应用编程
本文涵盖使用Python进行几何计算,如五边形面积、大圆距离及正多边形面积,同时涉及金融应用,包括金额分解、工资表计算及ASCII码字符显示。通过实例展示如何利用数学库进行精确计算,并结合Turtle库实现图形绘制。
import turtle
import math
import time

# 第3.2节
# 3.1(几何学:五边形的面积)

r = eval(input('Enter the length from the center to a vertex:'))
s = 2 * r * math.sin(math.pi / 5)
area = 5 * s * s / (4 * math.tan(math.pi / 5))
print(f'The area of the pentagon is {area:.2f}')

# 3.2 几何学:大圆距离

x1, y1 = eval(input('Enter point1 (latitude and longtitude) in degrees:'))
x2, y2 = eval(input('Enter point2 (latitude and longtitude) in degrees:'))
x1, y1, x2, y2 = math.radians(x1), math.radians(y1), math.radians(x2), math.radians(y2)
radius = 6371.01
d = radius * math.acos(math.sin(x1) * math.sin(x2) + math.cos(x1) * math.cos(x2) * math.cos(y1 - y2))
print(f'The distance between two points is {d} km')

# 3.3 几何学:估算面积,上不了外网,弃了

# 3.4 几何学:五角形的面积

s = eval(input('Enter the side:'))
area = (5 * s ** 2) / (4 * math.tan(math.pi / 5))
print(f'The area of the pentagon is {area}')

# 3.5 几何学:一个正多边形的面积

numberOfSides = eval(input('Enter the number of sides:'))
side = eval(input('Enter the side:'))
areaOfPolygon = (numberOfSides * side ** 2) / (4 * math.tan(math.pi / numberOfSides))
print(f'The area of the polygon is {areaOfPolygon}')

# 第3.3~3.6节
# 3.6 找出ASCII码的字符

asiiCode = eval(input('Enter an ASCII code:'))
char = chr(asiiCode)
print(f'The Character is {char}')

# 3.7 随机字符 ,未读懂题意

# 3.8 金融应用程序

amount = input('Enter an amount, for example, 1156:')
dollar = eval(amount[:-2])
cent = eval(amount[-2:])
quarter = cent // 25
remain = cent % 25
dime = remain // 10
remain = remain % 10
nickel = remain // 5
penny = remain % 5
print(f"Your amount {amount} consists of\n"
      f"\t{dollar} dollars\n"
      f"\t{quarter} quarters\n"
      f"\t{dime} dimes\n"
      f"\t{nickel} nickels\n"
      f"\t{penny} pennies")

# 3.9 金融应用程序:工资表

employee_name = input('Enter employee\'s name:')
hours_work = eval(input('Enter number of hours worked in a week:'))
pay_rate = eval(input('Enter hourly pay rate:'))
federal_tax_wh = eval(input("Enter federal tax withholding rate:"))
stats_tax_wh = eval(input("Enter state tax withholding rate:"))
gross_pay = hours_work * pay_rate
federal_wh = gross_pay * federal_tax_wh
states_wh = gross_pay * stats_tax_wh
print(f'Employee Name: {employee_name}\n'
      f'Hours Work: {hours_work:.1f}\n'
      f'Pay rate: ${pay_rate:.2f}\n'
      f'Gross Pay: ${gross_pay:.1f}\n'
      f'Deduction:\n'
      f'\tFederal Withholding(20%): ${federal_wh:.1f}\n'
      f'\tState Withholding(9.0%): ${states_wh:.2f}\n'
      f'\tTotal Deduction: ${federal_wh + states_wh:.2f}\n'
      f'Net Pay: ${gross_pay - (federal_wh + states_wh):.2f}')

# 3.10 Turtle:显示统一码

greece = '\u03b1\u03b2\u03b3\u03b4\u03b5\u03b6\u03b7\u03b8'
turtle.pensize()
turtle.write(greece, font=('Times', 20))
turtle.done()

# 3.11 反向数字

integer = input('Enter an integer:')
reversed_integer = integer[::-1]
print(f'The reversed number is {reversed_integer}')

# 3.12 Turtle:绘制一个五角星

sides = eval(input('Enter the side:'))
i = 0
while i <= 5:
    turtle.forward(100)
    turtle.right(144)
    i += 1
turtle.done()

# 3.13 Turtle:绘制一个STOP牌

turtle.begin_fill()
turtle.fillcolor('red')
turtle.left(60)
i = 0
while i < 6:
    turtle.forward(100)
    turtle.right(60)
    i += 1
turtle.end_fill()
turtle.right(60)
turtle.penup()
turtle.forward(100)
turtle.goto(100, -100 * math.sin(math.pi * 2 / 3) / 2)
turtle.pendown()
turtle.pencolor('white')
turtle.write('STOP', align='center', font=('Arial', 50, 'bold'))
turtle.done()

# 3.14 Turtle:绘制一个奥运五环标志

radius = eval(input('Enter the radius:'))


def oneLoop(r, color):
    turtle.pensize(6)
    turtle.color(color)
    turtle.circle(r)


def fiveCircle(r):
    turtle.penup()
    turtle.goto(r * 2 * -1 + r / 5 * -1, 0)
    turtle.pendown()
    oneLoop(r, "blue")
    turtle.penup()
    turtle.goto(0, 0)
    turtle.pendown()
    oneLoop(r, "black")
    turtle.penup()
    turtle.goto(r * 2 + r / 5, 0)
    turtle.pendown()
    oneLoop(r, "red")

    turtle.penup()
    turtle.goto((r + r / 10) * -1, r * -1)
    turtle.pendown()
    oneLoop(r, "yellow")

    turtle.penup()
    turtle.goto((r + r / 10), r * -1)
    turtle.pendown()
    oneLoop(r, "green")
    turtle.done()


fiveCircle(radius)

# 3.15 绘制一个笑脸

turtle.penup()
turtle.goto(0, -50)
turtle.pendown()
turtle.circle(100)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值