Python returns 错误 "can't assign to function call"

Python returns error "can't assign to function call"

我一直收到同样的错误。

这可能是一个简单的错误,我只是有一段时间没有编码了。

#import
import math

#initailse variables
float(x1)=0.0
float(y1)=0.0
float(x2)=0.0
float(y2)=0.0
float(complete_y)=0.0
float(complete_x)=0.0
float(final_decimal)=0.0
float(heading)=0.0

#get input
print("Welcome user!")
print("please enter your coordinates in format x and y including 
negatives")
x1=input()
y1=input()
print("please enter the coordinates of your target in the format x and y 
including negatives, you can obtain these from the map")
x2=input()
y2=input()
print("please hold for calculation")

#calculate
y2-y1==float(complete_y)
x2-x1==float(complete_x)
y/x==float(final_decimal)
math.asin(a)==float(heading)

#output
print("fire at a heading of", heading, "not including the negative if 
there is one")`enter code here`
print("press enter to close the programme")

导致错误

expected result is a heading in degrees.

你做不到

float(x)=0.0

您在值 x 上调用函数 float 并试图将 0.0 分配给该调用的结果,Python 不理解. Python 知道 0.0 的类型,你不必指定它是一个浮点数。就这样

x = 0.0  

此外,我不确定您希望 #calculate 部分做什么,但目前它不会以任何方式对程序的其余部分产生任何影响。

我的猜测是您在分配值之前试图声明类型,例如:

float(x)=0.0
x = input()

在python中,类型仅在运行时已知,您不能声明变量的类型。

如果你这样做:

x = 0.0
x = input()
print(type(x))

你会看到 x 是一个字符串,因为你用 input 方法分配了一个字符串。如果需要浮点数,需要进行转换

x = float(input())

作为旁注,

#calculate
y2-y1==float(complete_y)

如果之前声明了y1y2complete_y,此代码有效,它只是returns一个布尔值,TrueFalse.并且不要赋值。

如果需要赋值,使用单个=

complete_y = y2 - y1