Python 非整数 float

标签 python

好吧,为了这个问题我已经在办公 table 前敲了几天了,但我仍然无法理解我一直遇到这个问题:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 44, in <module>
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 35, in main
  File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 15, in __init__
builtins.TypeError: can't multiply sequence by non-int of type 'float'

一遍又一遍。我想我已经碰壁了,实际上我已经做了很多检查和测试,但如果有人能指出我正确的方向,我将不胜感激。

from math import pi, sin, cos, radians

def getInputs():
    a = input("Enter the launch angle (in degrees): ")
    v = input("Enter the initial velocity (in meters/sec): ")
    h = input("Enter the initial height (in meters): ")
    t = input("Enter the time interval between position calculations: ")
    return a,v,h,t
class Projectile:

    def __init__(self, angle, velocity, height):

        self.xpos = 0.0
        self.ypos = height
        theta =  pi *(angle)/180
        self.xvel = velocity * cos(theta)
        self.yvel = velocity * sin(theta)

    def update(self, time):
        self.xpos = self.xpos + time * self.xvel
        yvel1 = self.yvel - 9.8 * time
        self.ypos = self.ypos + time * (self.yvel + yvel1) / 2.0
        self.yvel = yvel1

    def getY(self):
        "Returns the y position (height) of this projectile."
        return self.ypos

    def getX(self):
        "Returns the x position (distance) of this projectile."
        return self.xpos

def main():
    a, v, h, t = getInputs()
    cball = Projectile(a, v, h)
    zenith = cball.getY()
    while cball.getY() >= 0:
        cball.update(t)
        if cball.getY() > zenith:
            zenith = cball.getY()
    print ("/n Distance traveled: {%0.1f} meters." % (cball.getY()))
    print ("The heighest the cannon ball reached was %0.1f meters." % (zenith))

if __name__ == "__main__": main()

最佳答案

您的输入函数返回字符串,而不是数字类型。您需要先根据需要将它们转换为整数或 float 。

我认为您遇到的特定错误是在您尝试计算 theta 时。您将 pi(一个 float )乘以 angle(包含一个字符串)。该消息告诉您不能将字符串乘以 float ,但可以将字符串乘以整数。 (例如 "spam"* 4 给你 "spamspamspamspam",但是 "spam"* 3.14 没有任何意义。)不幸的是,那不是一条非常有用的消息,因为对您来说错误的类型不是 pi,而是角度,它应该是一个数字。

您应该可以通过更改 getInputs 来解决此问题:

def getInputs():
    a = float(input("Enter the launch angle (in degrees): "))
    v = float(input("Enter the initial velocity (in meters/sec): "))
    h = float(input("Enter the initial height (in meters): "))
    t = float(input("Enter the time interval between position calculations: "))
    return a,v,h,t

我还应该指出,这是 Python 2.* 和 Python 3.* 具有不同行为的区域。在 Python 2.* 中,input 读取一行文本,然后将其作为 Python 表达式求值,而 raw_input 读取一行文本并返回一个字符串。在 Python 3.* 中,input 现在执行 raw_input 之前执行的操作 - 读取一行文本并返回一个字符串。虽然“将其作为表达式求值”的行为对于简单的示例可能会有帮助,但对于除微不足道的示例之外的任何其他示例都是危险的。用户可以输入任何表达式并对其求值,这可能会对您的程序或计算机做出各种意想不到的事情。

关于Python 非整数 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16266380/

相关文章:

python - Python 中的成对 Kullback Leibler(或 Jensen-Shannon)发散距离矩阵

python - 离线安装python & pip

python win32模拟点击

python - Pip 安装到错误的文件夹

python os.fdopen(os.open()) 不能用来写?

python 访问 raw_input 的值作为参数

python - 更改 pyodbc 期望 libodbc.2.dylib 存在的位置(更改默认 odbc 文件位置)

python - Flask-Migrate:alembic.util.CommandError:没有这样的修订

python - SQLAlchemy 过滤不工作

python - 如何让 tkinter 显示这些 un​​icode 字符?