类型:数值运算
描述
利用下面公式计算并输出 x 的值。
输入格式
在 3 行中分别输入一个浮点数(测试数据保证根号下的值大于或等于 0,且a不为零 )
输出格式
一个实数,严格保留小数点后2位数字。
示例
输入:
1
4
4
输出:
-2.00
解析:
- 有3行输入,各用一个input()函数
- 每行输入一个浮点数,用float()
- 表达式需要转为python计算表达式
- 不能省略乘号,乘法,开平方用math.sqrt() 或 x * 0.5
- 分母需要加括号
若用math.sqrt() ,需import math
严格保留小数点后2位需要用str.format()或f-string,round()会得到浮点数的最短表示
a=float(input())
b=float(input())
c=float(input())
x=(-b+(b*b-4*a*c)**0.5)/(2*a)
m=round(x,2)
print(m)
幂运算符号错,应用双星号 ```python a = float(input()) b = float(input()) c = float(input()) d = ((-b + (b 2 - 4 a c) 0.5) / (2 a)) print(‘{:.2f}’.format(d))
3. 括号不匹配,缺右侧括号;大括号需放引号中
![image.png](https://cdn.nlark.com/yuque/0/2022/png/1157448/1646749293278-04b00794-1ef0-43d1-ba06-6e1db41589a1.png#clientId=u1d7174a0-649c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=414&id=u08e0525d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=518&originWidth=1028&originalType=binary&ratio=1&rotation=0&showTitle=false&size=72637&status=done&style=none&taskId=ud2aaea8e-6800-4893-a770-974daa207a8&title=&width=822.4)<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/1157448/1646749364234-e3164853-3501-4c4a-b125-13d4f8d1fbf8.png#clientId=u1d7174a0-649c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=375&id=u36038bd1&margin=%5Bobject%20Object%5D&name=image.png&originHeight=469&originWidth=1127&originalType=binary&ratio=1&rotation=0&showTitle=false&size=57856&status=done&style=none&taskId=u00832ccb-9e1a-4ef0-9d83-3aa1d61f50b&title=&width=901.6)
3. 格式控制符号放到大括号里
![image.png](https://cdn.nlark.com/yuque/0/2022/png/1157448/1646749444674-d86c9902-9de7-4127-8c0d-d97453cbf69e.png#clientId=u1d7174a0-649c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=268&id=u09dec32b&margin=%5Bobject%20Object%5D&name=image.png&originHeight=335&originWidth=813&originalType=binary&ratio=1&rotation=0&showTitle=false&size=36863&status=done&style=none&taskId=ud961a177-5aae-4405-b68e-4884b30e150&title=&width=650.4)
4. f-string的字符串前必须有字符 f,缺少了f会原样输出完整字符串,不会代入变量值
```python
N=int(input())
P=int(input())
i=float(input())
F=P*(1+i)**N-P
print("利息={F:.2f}")
- str.format()中需正常输出的字符串要放到大括号外面,大括号里只放格式控制符号,不能放变量名和其他描述性字符串
p=int(input())
n=int(input())
i=float(input())
F=p*(1+i)**n
print("{利息=:.2f}".format(F))