Python:如果给定x,y或x + y2中的任何两个参数,我编写代码一切正常,直到x或y浮动为止

问题描述

我在这里编辑了代码,以分配要浮动的变量。它不起作用。

此代码用于查找(x + y)^ 2 = x ^ 2 + 2xy + y ^ 2中任一因子的值

有三个因素,x,y和结果。该值必须可用于任何两个。

这里唯一的问题是,如果x或y是浮动的,则找不到它们的值。为此,我期待在Google中使用我可以理解的数学库。

x = (input("\nEnter value of x: "))
y = (input("\nEnter value of y: "))
z = (input("\nEnter value of (x+y)^2: "))

#-----------------------------------------------------------------------
#Converting input to integers.
#-----------------------------------------------------------------------

a=0.0
b=0.0
c=0.0

if len(x)!=0:
     a = float(x)
else:
     print ("\nWe will search value of 'x'")

if len(y)!=0:
     b = float(y)
else:
     print ("\nWe will search value of 'y'")

if len(z)!=0:
     c = float(z)
else:
     print ("\nWe will search value of '(x+y)^2'")

#-----------------------------------------------------------------------
#Calculations
#-----------------------------------------------------------------------
     
from math import sqrt

fv=0.0 #fv = find value
rs=0.0 #rs = result

if len(x)==0:
     while True:
          rs = ((fv*fv) + (2*fv*b) + (b*b))
          fv = fv + 1
          if rs==c:
               print (f"\nValue of 'x' is either {(fv-1)} or {((sqrt(c)*-1) - b)}")
               break

if len(y)==0:
     while True:
          rs = ((a*a) + (2*a*fv) + (fv*fv))
          fv = fv+1
          if rs==c:
               print (f"\nValue of 'y' is either {(fv-1)} or {((sqrt(c)*-1) - a)}")
               break

if len(z)==0:
     rs = ((a*a) + (2*a*b) + (b*b))
     print (f"\nValue of '(x+y)^2' is {rs}")


print ("\nThank you")

解决方法

尝试

print ("\n\nEnter values in below equations. \nIf you don't know the value,than press \"Enter\" ")

x = (input("\nEnter value of x: "))
y = (input("\nEnter value of y: "))
z = (input("\nEnter value of (x+y)^2: "))

#-----------------------------------------------------------------------
#Converting input to integers.
#-----------------------------------------------------------------------

a=0
b=0
c=0

if len(x)!=0:
     a = float(x)
else:
     print ("\nWe will search value of 'x'")

if len(y)!=0:
     b = float(y)
else:
     print ("\nWe will search value of 'y'")

if len(z)!=0:
     c = int(z)
else:
     print ("\nWe will search value of '(x+y)^2'")

#-----------------------------------------------------------------------
#Calculations
#-----------------------------------------------------------------------
     
from math import sqrt

fv=0 #fv = find value
rs=0 #rs = result

if len(x)==0:
     while True:
          rs = ((fv*fv) + (2*fv*b) + (b*b))
          fv = fv + 1
          if rs==c:
               print (f"\nValue of 'x' is either {(fv-1)} or {int((sqrt(c)*-1) - b)}")
               break

if len(y)==0:
     while True:
          rs = ((a*a) + (2*a*fv) + (fv*fv))
          fv = fv+1
          if rs==c:
               print (f"\nValue of 'y' is either {(fv-1)} or {int((sqrt(c)*-1) - a)}")
               break

if len(z)==0:
     rs = ((a*a) + (2*a*b) + (b*b))
     print (f"\nValue of '(x+y)^2' is {rs}")


print ("\nThank you")

只需将int更改为float

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...