类型:数值运算‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬


    描述‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    根据下面公式计算并输出x的值,a和b的值由用户输入,括号里的数字是角度值, 要求圆周率的值使用数学常数math.pi,开平方使用math库中开平方函数,三角函数的值用math库中对应的函数进行计算 。
    image.png
    输入格式
    输入包括两行, 每行一个数字。
    输出格式
    表达式的值‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    示例‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    输入:
    2
    5
    输出:
    -0.9209814967618768

    解析:

    1. 有两行输入,各用一个input()函数
    2. 每行输入一个数字,未说明数值类型时用浮点数float()
    3. 表达式需要转为python计算表达式
    4. 不能省略乘号
    5. 三角函数的参数应为弧度
    6. 分母需要加括号
    7. 若用pi,需import math

    常见问题

    1. 导入函数时应该只导入函数名,不加括号

    image.png

    1. a=int(input())
    2. b=int(input())
    3. from math import sin(),cos()
    4. x=(-b+(2*a*sin(1/6)*cos(1/6)**0.5))
    5. print(x)


    2. 三角函数参数应为弧度,角度要转为弧度

    1. import math
    2. c=math.sin(60)*math.cos(60)
    3. c=2*a*c
    4. c=c**0.5
    5. a=(c-b)/(2*a)
    6. print(a)
    1. a=int(input())
    2. b=int(input())
    3. x=-b+2a*sin(60)*cos(60)**(1/2)/2*a
    4. print(x)
    1. a=float(input())
    2. b=float(input())
    3. import math
    4. x=(-b+((2*a*math.sin(60)*math.cos(60))**0.5))/(2*a)
    5. print(x)
    1. import math
    2. a = int(input())
    3. b = int(input())
    4. x = ((-b + (2 * a * math.sin(1/3) * math.cos(1/3)) ** 0.5) / (2 * a)) # 括号中参数应为弧度
    5. print(x)
    1. 缺少乘号

    image.png

    1. a=int(input())
    2. b=int(input())
    3. x=-b+2a*sin(60)*cos(60)**(1/2)/2*a
    4. print(x)
    1. 分母缺少括号
      1. a=int(input())
      2. b=int(input())
      3. x=-b+2a*sin(60)*cos(60)**(1/2)/2*a
      4. print(x)
    1. import math
    2. a=eval(input())
    3. b=eval(input())
    4. x=(-b+math.sqrt(2*a*math.sin(math.pi/3)*math.cos(math.pi/3)))/2*a
    5. print(x)
    1. 题目未说明是整数时,输入应该转为浮点数

      1. import math
      2. a=int(input()) # float()
      3. b=int(input())
      4. x=(-b+(2*a*math.sin(math.pi/3)*math.cos(math.pi/3))**0.5)/(2*a)
      5. print(x)
      1. import math
      2. a=int(input())
      3. b=int(input())
      4. c=2*a*math.sin(math.pi/3)*math.cos(math.pi/3)
      5. d=c**0.5
      6. x=(-b+d)/(2*a)
      7. print(x)
      1. import math
      2. a=int(input()) # 浮点数
      3. b=int(input())
      4. x=(-b+(2*a*math.sin(math.pi/3)*math.cos(math.pi/3))**0.5)/(2*a)
      5. print(x)
    2. 缺少输入语句 ```python import math

    a=int(2) # a,b应由用户输入 b=int(5) print((-b+math.sqrt(2amath.sin(math.pi/3)math.cos(math.pi/3)) )/(2a))

    1. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/1157448/1646748905755-6e916b2b-9a8a-4a8d-aff6-4ae5c4d86548.png#clientId=ud9e2c9e5-daec-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=302&id=u935d1304&margin=%5Bobject%20Object%5D&name=image.png&originHeight=377&originWidth=664&originalType=binary&ratio=1&rotation=0&showTitle=false&size=41846&status=done&style=none&taskId=u7fd6642c-d164-4f4f-900b-c1f773bffc2&title=&width=531.2)
    2. ```python
    3. import math
    4. c=math.sin(60)*math.cos(60)
    5. c=2*a*c
    6. c=c**0.5
    7. a=(c-b)/(2*a)
    8. print(a)
    1. import math
    2. x = (-b+(2*A*sin((math.pi)/3)*cos((math.pi)/3))**0.5)/(2*a) # A未定义
    3. print(x)
    1. 定义太多变量名,程序可读性变差

      1. import math
      2. a=int(input())
      3. b=int(input())
      4. c=2*a*math.sin(math.pi/3)*math.cos(math.pi/3)
      5. d=c**0.5
      6. x=(-b+d)/(2*a)
      7. print(x)
    2. 函数应用错误,此题应用正余弦函数

      1. a=eval(input())
      2. b=eval(input())
      3. print(-b+math.pow(2*a*math.radians(60)*math.radians(60),0.5)/2/a)
    3. 角度转纸弧度出错

      1. a=float(imput())
      2. b=float(imput())
      3. from math import sin,cos
      4. x=(-b+(2*a*sin(1/6)*cos(1/6))**0.5)/2*a # 参数应为math.pi/3
      5. print(x)
      1. a=float(input())
      2. b=float(input())
      3. from math import *
      4. x=(-b+(2*a*sin(60)*cos(60))**0.5)/(2*a)
      5. print(x)
    4. 开平方处括号位置错 ```python a = eval(input()) b = eval(input()) import math print(-b + math.sqrt(2 a math.sin(math.pi / 3) math.cos(math.pi / 3) / (2 a)))

    1. 11. 重复用相同变量名导致混乱
    2. ```python
    3. import math
    4. c=math.sin(60)*math.cos(60)
    5. c=2*a*c
    6. c=c**0.5
    7. a=(c-b)/(2*a)
    8. print(a)
    1. 多种错误

    image.png
    13.括号位置错误
    print()的括号到最后一个除号前结束,print()函数的返回值是None,结果导致None/(2*a),语法错误

    1. import math
    2. a=float(input())
    3. b=float(input())
    4. print(-b+(2*a*math.sin(math.pi/3)*math.cos(math.pi/3))**0.5)/(2*a) # print()的括号到最后一个除号前结束
    5. # TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'
    1. 不同计算方法会导致精度问题,请严格按题目描述调用正余弦函数计算,不要直接代入函数值
      1. from math import sqrt
      2. a=int(input())
      3. b=int(input())
      4. x=(-b+sqrt(2*a*sqrt(3)/2*0.5))/2/a
      5. print(x)