Python返回错误“无法分配给函数调用”

2024-04-19 13:08:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我总是犯同样的错误

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

#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.


Tags: theininput错误floatcompleteprintx1
2条回答

我的猜测是,您需要在赋值之前声明类型,例如:

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,则此代码有效,它只返回布尔值TrueFalse。不要赋值

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

complete_y = y2 - y1

你不能这么做

float(x)=0.0

您正在对值x调用函数float,并尝试将0.0赋值给该调用的结果,而Python无法理解。Python知道0.0的类型,您不必指定它是一个float。就这么做吧

x = 0.0  

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

相关问题 更多 >