简体   繁体   中英

Python non-int float

Alright so I have beat my head over my desk for a few days over this one and I still cannot get it I keep getting this problem:

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'

over and over. I think I have hit the wall and really I have done a ecent amount of looking and testing but if anyone could point me in teh right direction it would be greatly appreciated.

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()

Your input functions return strings, not numeric types. You need to convert them to integers or floats first as appropriate.

I think the particular error you're seeing is when you try to calculate theta. You multiply pi (a floating-point number) by angle (which holds a string). The message is telling you that you can't multiply a string by a float, but that you could multiply a string by an int. (Eg "spam" * 4 gives you "spamspamspamspam" , but "spam" * 3.14 would make no sense.) Unfortunately, that's not a very helpful message, because for you it's not pi that's the wrong type, but angle, which should be a number.

You should be able to fix this by changing 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

I should also note that this is an area where Python 2.* and Python 3.* have different behaviour. In Python 2.* , input read a line of text and then evaluated it as a Python expression, while raw_input read a line of text and returned a string. In Python 3.* , input now does what raw_input did before - reads a line of text and returns a string. While the 'evaluate it as an expression' behaviour could be helpful for simple examples, it was also dangerous for anything but a trivial example. A user could type in any expression at all and it would be evaluated, which could do all sorts of unexpected things to your program or computer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM