函数pay()练习题1.0

21 阅读2分钟

题目1:编写函数pay(),带两个输入参数:小时工资和上周员工工作了的小时数。函数计算并返回员工的工资。 加班工资的计算方法如下:大于40小时但小于或等于60小时按平时小时薪酬的1.5倍给薪;大于60小时则按平时小时薪酬的2倍给薪。 函数接口定义: pay(salaryHour, hours) 注:salaryHour是平时小时薪酬,hours是员工工作的小时数

def pay(salaryHour,hours):
    salary = 0
    if hours <= 40:
        salary = salaryHour * hours
    elif hours <= 60:  # 40 < hours <= 60
        salary = salaryHour * 40 + (hours - 40) * salaryHour * 1.5
    else:  # hours > 60
        salary = salaryHour * 40 + salaryHour * 1.5 * 20 + salaryHour * 2 * (hours - 60)
    return salary
# 测试用例
hours = 65
salaryHour = 40
print(f"工作了{hours}小时,每小时工资为{salaryHour}元,总的工作收入为{pay(salaryHour,hours)}元")

题目2:输入三个正实数(浮点数),如果能够组成三角形,输出三角形面积,如果不能,输出"error"。 三个数能够组成三角形的条件是:任意两边之和大于第三边。 已知三边长求三角形面积的公式为: s=(a+b+c)/2 area=sqrt(s(s−a)(s−b)(s−c))

import math
a = float(input("请输入正实数a:"))
b = float(input("请输入正实数b:"))
c = float(input("请输入正实数c:"))
if a + b > c:  # 满足构成三角形的条件
    # 计算三角形面积
    s = (a + b + c) / 2
    area = math.sqrt( s * ( s - a ) * ( s - b ) * ( s - c ))
    print(f"三边边长为{a}{b}{c}的三角形的面积为{area:.2f}")

题目3:创建正方形画布,以画布中心为原点画出坐标轴,并按以下公式绘制函数曲线: x = wh × ( cos(t) + (1/2) × cos(7t) + (1/3) × sin(17t) ) / 2 y = hh × ( sin(t) + (1/2) × sin(7t) + (1/3) × cos(17t) ) / 2 其中wh、hh的取值分别为画布的半宽和半高,t的取值范围为0至2π,步长为0.01.

import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0,np.pi * 2, 0.01)
wh = 100
hh = 100
x = wh * (np.cos(t) + 1/2 * np.cos(7*t) + 1/3 * np.sin(17*t)) / 2
y = hh * (np.sin(t) + 1/2 * np.sin(7*t) + 1/3 * np.cos(17*t)) / 2
plt.subplots(figsize=(wh,hh))
plt.plot(x,y)
plt.grid() # 网格
plt.show()