1

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()
6
  • You'll be wanting raw_input rather than input Commented Apr 28, 2013 at 18:45
  • 1
    As @DavidHeffernan noted, you might find this helpful stackoverflow.com/questions/4915361/… Commented Apr 28, 2013 at 18:46
  • I had no problem with launching the code... Commented Apr 28, 2013 at 18:47
  • 1
    @DavidHeffernan I think the OP is using py3x, where raw_input has been replaced by input Commented Apr 28, 2013 at 18:47
  • I have python 2.7.2, and it has both input and raw_input Commented Apr 28, 2013 at 18:50

1 Answer 1

8

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. (E.g. "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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ahh thank you very much for taking the time to respond to me, I really do appreciate it. I did what you said and now I am getting. 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 34, in main File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver_sandbox.py", line 6, in getInputs builtins.ValueError: could not convert string to float: which while it is refreshing to get a new message, any thoughts?
Perhaps you didn't type in a number?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.