小编Rak*_*lam的帖子

无法在 python 3 上将复数转换为浮点数

我为 uri 在线法官编写此代码(问题编号 1036)...这是 Bhaskara 公式...

import cmath
A,B,C=input().split()
A = float(A)
B = float(B)
C = float(C)
D = (B*B)-(4*A*C)
if((D== -D)|(A==0)):
    print("Impossivel calcular")

else:
    T = cmath.sqrt(D)
    x1 = (-B+T)/(2*A)
    x2 = (-B-T)/(2*A)
    print("R1 = %.5f" %x1)
    print("R2 = %.5f" %x2)
Run Code Online (Sandbox Code Playgroud)

但是当我提交这个程序时...发生了运行时错误...

Traceback (most recent call last): File "Main.py", line 14, in
    print("R1 = %.5f" %x1)
TypeError: can't convert complex to float
Command exited with non-zero status (1)
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题。

python-3.x

6
推荐指数
1
解决办法
4万
查看次数

我如何使用浮点数在Python的for循环中进行增量

我想在for循环中使用浮点数。所以我写了这段代码:

k=1
for i in range(0,3,0.2):
    for j in range(k,k+3,1):
        print("I= %i J=%i"%(i,j))
        k=k+0.2
Run Code Online (Sandbox Code Playgroud)

但出现了以下错误:

Traceback (most recent call last):
  File "C:\Users\Md. Rakibul Islam\Desktop\URI practise.py", line 2, in <module>
   for i in range(0,3,0.2):
TypeError: 'float' object cannot be interpreted as an integer
Run Code Online (Sandbox Code Playgroud)

python-3.x

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

python-3.x ×2