Solve Lambda Error in Python Homework

In summary: It looks like it's trying to get the lambda function from some other source, but can't seem to find it.In summary, the problem is that someone's written a function named lambda and they're trying to input 5 arguments into it, but instead are inputting 3.
  • #1
BiGyElLoWhAt
Gold Member
1,622
131

Homework Statement


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.

Homework Equations



Quite a few
Code:
# -*- 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:
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)

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.
 
Technology news on Phys.org
  • #2
It didn't like my #'s in the code. It tried to latex them.
 
  • #3
BiGyElLoWhAt said:
So where is it getting this lambda thing from?
input() takes a single argument. As you have used it, there are five args.

Try this:
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.
 
  • Like
Likes BiGyElLoWhAt
  • #4
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?
 
  • #5
BiGyElLoWhAt said:
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?
What other errors and what input?
 
  • #6
BiGyElLoWhAt said:
CoM1 = distance/(1+Mass1/Mass2) (Comment pound-pound) Distance to center of mass of system
Also, for comments, use just a single # character. With two of them, the browser interprets this as some LaTeX script.
 
  • #7
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
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.
 
  • #8
I suppose I didn't put a space as input. I just hit enter.
 
  • #9
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.
 
  • #10
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:
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>
 
  • #11
I hit space and enter and got this:
Code:
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
 
  • #12
I am using python 2.7 though. Perhaps that has something to do with it.
 
  • #13
I suppose it's all just flare, anyways. It'd be nice to know what the deal is, but it's absolutely non-essential.
 
  • #14
BiGyElLoWhAt said:
I suppose it's all just flare
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.
 
  • #15
Yea, that's probably the one that I meant. Not the one that you shoot up in the air.
Sorry, passed out last night.
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:
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
 
  • #16
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/.
 
  • #17
  • #18
You should also check the documentation for Python 2.7.
 

1. How do I identify and fix a Lambda error in my Python homework?

To identify a Lambda error, you should first check the error message that is displayed when your code fails to run. This message will often provide a clue as to what caused the error. Some common fixes for Lambda errors include checking for typos, ensuring correct syntax, and importing necessary libraries.

2. Why am I getting a "Lambda function not found" error?

This error usually means that you have not defined a Lambda function in your code or that the function is not named correctly. Make sure that you have declared a Lambda function and that its name matches the name used in your code.

3. How do I troubleshoot a Lambda timeout error?

A Lambda timeout error occurs when your function takes longer to run than the time allotted for it. To troubleshoot this error, you can try optimizing your code to make it run faster, or you can increase the timeout limit for your Lambda function.

4. What should I do if my Lambda function is not executing properly?

If your Lambda function is not executing properly, it could be due to a variety of reasons such as incorrect permissions, missing dependencies, or incorrect function configuration. You can try checking these factors and making any necessary changes to ensure that your function runs correctly.

5. Is there a way to test my Lambda function before deploying it?

Yes, you can use the Lambda console to test your function before deploying it. This allows you to catch any errors and make necessary changes before your code is live. You can also use tools like Postman to test your function's API endpoints.

Similar threads

  • Programming and Computer Science
Replies
4
Views
4K
Back
Top