Python Solve Lambda Error in Python Homework

AI Thread Summary
The discussion revolves around a Python script intended to simulate redshift in a star system, but the user encounters a TypeError related to the input function. The error arises from using the input function incorrectly, where multiple arguments are passed instead of a single string. The suggestion is made to replace the problematic input lines with print statements followed by a single input call to pause execution. The user also faces additional errors when attempting to run the program mid-execution, particularly with input handling, which leads to confusion about the validity of input values. The conversation highlights differences between Python 2.7 and 3.x, particularly in how print and input functions are handled. The final code version provided resolves some issues, but the user still experiences syntax errors, prompting further troubleshooting and recommendations to consult Python documentation for clarification on input handling.
BiGyElLoWhAt
Gold Member
Messages
1,637
Reaction score
138

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
It didn't like my #'s in the code. It tried to latex them.
 
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
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?
 
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?
 
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.
 
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.
 
I suppose I didn't put a space as input. I just hit enter.
 
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.
 

Similar threads

Back
Top