Solve Lambda Error in Python Homework

  • Context: Python 
  • Thread starter Thread starter BiGyElLoWhAt
  • Start date Start date
  • Tags Tags
    error lambda python
Click For Summary

Discussion Overview

The discussion revolves around a Python homework problem involving the simulation of redshift for a star system. Participants are addressing a TypeError related to the use of the input function and its arguments within the code.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes encountering a TypeError indicating that a lambda function takes at most one argument, despite their code not explicitly using a lambda function.
  • Another participant suggests that the error arises from the use of the input function with multiple arguments, recommending a change to use print statements followed by a single input call.
  • Some participants express confusion about the appropriateness of using input mid-program, questioning whether this is standard practice in Python.
  • There are mentions of various errors encountered, including SyntaxError and NameError, with participants speculating on their causes, such as missing parentheses or incorrect input types.
  • One participant notes that commenting out input statements allows the code to run without errors, raising questions about the necessity and functionality of input prompts in the code.
  • Another participant shares their successful execution of the code with valid inputs, but expresses uncertainty about the reasonableness of the output.
  • There is a discussion about differences between Python 2.7 and Python 3.x, particularly regarding the syntax of print statements and how it may affect code execution.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the source of the errors or the best approach to resolve them. Multiple competing views on the use of input and error handling remain present throughout the discussion.

Contextual Notes

Participants note potential limitations in their understanding of Python syntax and behavior, particularly regarding the differences between versions 2.7 and 3.x. There are also unresolved issues related to specific error messages and their causes.

Who May Find This Useful

This discussion may be useful for students or individuals working on Python programming, particularly those encountering similar issues with input handling and error messages in their code.

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   Reactions: 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

  • · Replies 4 ·
Replies
4
Views
4K