1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Lambda error in python

  1. Mar 14, 2016 #1

    BiGyElLoWhAt

    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data
    Somewhat irrelevant, but we need to simulate the redshift for a star system. Pretty open ended. Single star, binary, whatever.
    The problem is this:
    TypeError: <lambda>() takes at most 1 argument (5 given)
    And upon searching, most people who get this error have a function that they're using named lambda and it takes some number of arguments and they input the wrong ones such as here:
    http://stackoverflow.com/questions/31447844/typeerror-lambda-takes-exactly-1-argument-3-given
    That's not the case with mine.

    2. Relevant equations

    Quite a few
    Code (Text):
    # -*- coding: utf-8 -*-
    """
    Created on Mon Mar 14 20:49:29 2016

    @author: Tyler
    """

    from __future__ import division
    from math import sin, cos, sqrt, pi
    step = float(input("Enter the step size in seconds: "))
    G = 6.674*10**(-11)
    c = 3*10**8
    def Move_Stars(r, theta):
        x= r*cos(theta + step)
        y = r*sin(theta + step)
        return x,y
    Mass1 = int(input("Enter the mass of object 1 in Kg: "))
    Mass2 = int(input("Enter the mass of object 2 in Kg: "))
    x_obs = float(input("Enter the x coordinate of the observer in m: "))
    y_obs = float(input("Enter the y coordinate of the observer in m: "))
    distance = float(input("Enter the distance between the two stars: "))

    CoM1 = distance/(1+Mass1/Mass2) (Comment pound-pound) Distance to center of mass of system
    CoM2 = distance/(1+Mass2/Mass1) (Comment pound-pound) Distance to center of mass of system
    speed1 = sqrt(G*Mass2/CoM1)
    speed2 = sqrt(G*Mass1/CoM2)

    XS1 = CoM1
    XS2 = -CoM2
    YS1, YS2 = 0.0, 0.0
    input("The speed and orbital radius of Object 1 are ", speed1, " and ", CoM1 , " respectively. Input any key to continue. ")
    input("The speed and orbital radius of Object 2 are ", speed2, " and ", CoM2 , " respectively. Input any key to continue. ")
    timeLapse = float(input("Enter the time you would like to watch over in seconds: "))
    time = 0
    omega = speed1/CoM1
    theta1 = 0
    theta2 = pi
    z=0
    Rs1 = 2*G*Mass1/(c**2)

    while time < timeLapse:
        XS1, YS1 = Move_Stars(CoM1, theta1)
        XS2, YS2 = Move_Stars(CoM2, theta2)
        theta1 += omega*step
        theta2 += omega*step
        print("Position 1 = (",XS1, ",",YS1, ")")
        print("Position 1 = (",XS2, ",",YS2, ")")
        time += step
    Sorry for the lack of comments.
    It works fine up until this:
    Code (Python):
    Enter the distance between the two stars: 100000
    Traceback (most recent call last):

      File "<ipython-input-2-713c37472433>", line 1, in <module>
        runfile('C:/Python27/Lib/site-packages/xy/Compu2Modern.py', wdir='C:/Python27/Lib/site-packages/xy')

      File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
        execfile(filename, namespace)

      File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
        exec(compile(scripttext, filename, 'exec'), glob, loc)

      File "C:/Python27/Lib/site-packages/xy/Compu2Modern.py", line 31, in <module>
        input("The speed and orbital radius of Object 1 are ", speed1, " and ", CoM1 , " respectively. Input any key to continue. ")

    TypeError: <lambda>() takes at most 1 argument (5 given)
     
    3. The attempt at a solution
    So where is it getting this lambda thing from?
    To me, it looks like it's error-ing on line 31, which is nothing. Just an arbitrary input, not assigned to any variable, to give the user time to read some info before it starts running the loop.
     
  2. jcsd
  3. Mar 14, 2016 #2

    BiGyElLoWhAt

    User Avatar
    Gold Member

    It didn't like my #'s in the code. It tried to latex them.
     
  4. Mar 14, 2016 #3

    Mark44

    Staff: Mentor

    input() takes a single argument. As you have used it, there are five args.

    Try this:
    Code (Python):
    print("The speed and orbital radius of Object 1 are ", speed1, " and ", CoM1 , " respectively.")
    input("Press any key to continue.")
    Don't forget you need to fix the other call to input() as well.
     
  5. Mar 14, 2016 #4

    BiGyElLoWhAt

    User Avatar
    Gold Member

    Ahhh that makes sense, I suppose.

    So now I'm getting other errors. I'm assuming it's because it doesn't like my inputs that I'm giving it. I've never done this mid program before, only at the end, to keep it from terminating before you can get the output info. Is this an acceptable thing to do according to python?
     
  6. Mar 14, 2016 #5

    Mark44

    Staff: Mentor

    What other errors and what input?
     
  7. Mar 14, 2016 #6

    Mark44

    Staff: Mentor

    Also, for comments, use just a single # character. With two of them, the browser interprets this as some LaTeX script.
     
  8. Mar 14, 2016 #7

    BiGyElLoWhAt

    User Avatar
    Gold Member

    first error, with a space as input:
    SyntaxError: unexpected EOF while parsing

    second error, with n as input:
    NameError: name 'n' is not defined

    for the EOF error, google told me to look for a missing parenthesis
    Code (Python):
    print("The speed and orbital radius of Object 1 are ", speed1, " and ", CoM1 , " respectively.")
    input("Input any key to continue. ")
    print("The speed and orbital radius of Object 2 are ", speed2, " and ", CoM2 , " respectively.")
    input("Input any key to continue. ")
    It gets to the first input, then errors upon my submission of said input.
     
  9. Mar 14, 2016 #8

    BiGyElLoWhAt

    User Avatar
    Gold Member

    I suppose I didn't put a space as input. I just hit enter.
     
  10. Mar 14, 2016 #9

    BiGyElLoWhAt

    User Avatar
    Gold Member

    I commented out the inputs, and it ran fine. I'm just curious why you can't do this, or if I'm just doing it in a dumb way.
     
  11. Mar 14, 2016 #10

    Mark44

    Staff: Mentor

    I'm not getting any errors. Here's the output I get. I don't know if my input values are valid, so I can't tell if the output I get is reasonable.
    For the input prompts, I pressed SPACE and then hit ENTER.
    Code (Text):

    C:\Users\Mark\Documents\Python3.4.2>python test.py
    Enter the step size in seconds: 10
    Enter the mass of object 1 in Kg: 10000
    Enter the mass of object 2 in Kg: 20000
    Enter the x coordinate of the observer in m: 0
    Enter the y coordinate of the observer in m: 0
    Enter the distance between the two stars: 50000
    The speed and orbital radius of Object 1 are  6.3280328696997135e-06  and  33333.333333333336  respectively.
    Press any key to continue.
    The speed and orbital radius of Object 2 are  6.3280328696997135e-06  and  16666.666666666668  respectively.
    Press any key to continue.
    Enter the time you would like to watch over in seconds: 20
    Position 1 = ( -27969.050969215084 , -18134.03702964566 )
    Position 1 = ( 13984.525484607544 , 9067.018514822828 )
    Position 1 = ( -27969.05093478924 , -18134.037082742394 )
    Position 1 = ( 13984.525467394622 , 9067.018541371197 )

    C:\Users\Mark\Documents\Python3.4.2>
     
  12. Mar 14, 2016 #11

    BiGyElLoWhAt

    User Avatar
    Gold Member

    I hit space and enter and got this:
    Code (Text):
    Enter the step size in seconds: 10

    Enter the mass of object 1 in Kg: 100000

    Enter the mass of object 2 in Kg: 500000

    Enter the x coordinate of the observer in m: 0

    Enter the y coordinate of the observer in m: 0

    Enter the distance between the two stars: 2000000
    ('The speed and orbital radius of Object 1 are ', 4.474594953736036e-06, ' and ', 1666666.6666666667, ' respectively.')

    Input any key to continue.
      File "<string>", line unknown
       
        ^
    SyntaxError: unexpected EOF while parsing
     
     
  13. Mar 14, 2016 #12

    BiGyElLoWhAt

    User Avatar
    Gold Member

    I am using python 2.7 though. Perhaps that has something to do with it.
     
  14. Mar 14, 2016 #13

    BiGyElLoWhAt

    User Avatar
    Gold Member

    I suppose it's all just flare, anyways. It'd be nice to know what the deal is, but it's absolutely non-essential.
     
  15. Mar 14, 2016 #14

    Mark44

    Staff: Mentor

    You mean "flair"?
    I was able to get your code to run, but I'm using 3.4.2. There's a difference between the 2.7 versions and the 3.x versions in that print doesn't take parentheses in the earlier versions. Other than that, I can't think of any reason why your code doesn't run for you, but it does for me.

    What's your final version look like? Possibly you're missing a quote, or you have one where it's unexpected in the earlier version.
     
  16. Mar 15, 2016 #15

    BiGyElLoWhAt

    User Avatar
    Gold Member

    Yea, that's probably the one that I meant. Not the one that you shoot up in the air.
    Sorry, passed out last night.
    Code (Python):
    # -*- coding: utf-8 -*-
    """
    Created on Mon Mar 14 20:49:29 2016

    @author: Tyler
    """


    from __future__ import division
    from math import sin, cos, sqrt, pi
    step = float(input("Enter the step size in seconds: "))
    G = 6.674*10**(-11)
    c = 3*10**8
    def Move_Stars(r, theta):
        x= r*cos(theta + step)
        y = r*sin(theta + step)
        return x,y
    Mass1 = int(input("Enter the mass of object 1 in Kg: "))
    Mass2 = int(input("Enter the mass of object 2 in Kg: "))
    x_obs = float(input("Enter the x coordinate of the observer in m: "))
    y_obs = float(input("Enter the y coordinate of the observer in m: "))
    distance = float(input("Enter the distance between the two stars: "))

    CoM1 = distance/(1+Mass1/Mass2) #Distance to center of mass of system
    CoM2 = distance/(1+Mass2/Mass1) #Distance to center of mass of system
    speed1 = sqrt(G*Mass2/CoM1)
    speed2 = sqrt(G*Mass1/CoM2)

    XS1 = CoM1
    XS2 = -CoM2
    YS1, YS2 = 0.0, 0.0
    print("The speed and orbital radius of Object 1 are ", speed1, " and ", CoM1 , " respectively.")
    input("Input any key to continue. ")
    print("The speed and orbital radius of Object 2 are ", speed2, " and ", CoM2 , " respectively.")
    input("Input any key to continue. ")
    timeLapse = float(input("Enter the time you would like to watch over in seconds: "))
    time = 0
    omega = speed1/CoM1
    theta1 = 0
    theta2 = pi
    z=0
    Rs1 = 2*G*Mass1/(c**2)
    Rs2 = 2*G*Mass2/(c**2)

    while time < timeLapse:
        XS1, YS1 = Move_Stars(CoM1, theta1)
        XS2, YS2 = Move_Stars(CoM2, theta2)
        theta1 += omega*step
        theta2 += omega*step
        print("Position 1 = (",XS1, ",",YS1, ")")
        print("Position 2 = (",XS2, ",",YS2, ")")
        time += step
       
    There's my code.
    I might just be blind, but I'm not seeing anything to the effect of parentheses or quotes, etc.
    Additionally, here's my console return:
    Code (Text):
    runfile('C:/Python27/Lib/site-packages/xy/Compu2Modern.py', wdir='C:/Python27/Lib/site-packages/xy')

    Enter the step size in seconds: 10

    Enter the mass of object 1 in Kg: 100000

    Enter the mass of object 2 in Kg: 500000

    Enter the x coordinate of the observer in m: 0

    Enter the y coordinate of the observer in m: 0

    Enter the distance between the two stars: 2000000
    ('The speed and orbital radius of Object 1 are ', 4.474594953736036e-06, ' and ', 1666666.6666666667, ' respectively.')

    Input any key to continue.
      File "<string>", line unknown
       
        ^
    SyntaxError: unexpected EOF while parsing
     
     
  17. Mar 15, 2016 #16

    Mark44

    Staff: Mentor

    Your code seems to be failing at the first input line. See if this helps -- change the two input() lines to s = input("...")

    Other than that, I don't have any more ideas. Your code works for me running 3.4.2. If you still can't get it to work, you might try posting a question on http://www.python.org/.
     
  18. Mar 15, 2016 #17

    BiGyElLoWhAt

    User Avatar
    Gold Member

  19. Mar 15, 2016 #18

    Mark44

    Staff: Mentor

    You should also check the documentation for Python 2.7.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Lambda error in python
  1. Python array error (Replies: 5)

Loading...