Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类型错误需要浮点,Python错误_Python - Fatal编程技术网

类型错误需要浮点,Python错误

类型错误需要浮点,Python错误,python,Python,怎么了 from random import randint from math import sin from math import cos from decimal import Decimal gravity = 32 target = randint(1,5280) print("Your target is " + str(target)) velocity = raw_input("Enter what velocity: ") angle = raw_input(

怎么了

from random import randint

from math import sin

from math import cos

from decimal import Decimal

gravity = 32

target = randint(1,5280)

print("Your target is " + str(target))

velocity = raw_input("Enter what velocity: ")

angle = raw_input("Enter what angle: ")

time = (2 * (velocity(sin(angle)))/gravity)

distance = (velocity(cos(angle))*time)

print(str(time))

您需要强制转换为浮动:

angle = float(raw_input("Enter what angle: ")) # same for velocity

你遇到的错误是角度应该是一个浮点,但是你从原始输入中得到它,顾名思义,它返回原始输入,即字符串。因为您假设输入是一个正确的浮点整数,所以可以继续使用

eval(原始输入(“提示”))

对于这两个数字。然后你会遇到第二个问题,那就是
速度(sin(角度))

将velocity视为一个函数,在将其定义为浮点之后,还没有将其视为一个函数。如果要使用函数,请确保使用不同的名称,如果要乘以它,则应使用
velocity*无论什么

简要说明:
eval()如果只让任何人输入,可能会非常危险,因为它将输入解释为python代码,因此小心使用它

转换为
float
可能会更好。是的,我刚刚更改了它。看起来像一个物理问题,我们需要大量有效数字。您使用的括号就像函数调用一样,而不是乘法。
时间=(2*(速度*sin(角度))/gravity)
距离=(速度*cos(角度)*时间)