How to assign two function return values to two variables and use them in the same line?

Question:

Right, so this might be a rather confusing question. But in my Computer science class, we were given this optional challenge to calculate the distances a catapult has achieved after a user inputs a set of angles and speeds. But the challenge is that we have to split the problem into multiple smaller functions but each function is only allowed to have one statement per function.

I think I have a way to do it but I’m having trouble with one part.

[x = get_angles(),  y = get_speeds(): 2*x[i]*y[i]/GRAVITY for i in range(len(x))]

This is part of my code for creating a list of the distances travelled. Now, this is effectively pseudo-code cause I have no clue how to make it work. Does python have anything that allows something like this to happen?

Any help would be great. Sorry for being so long-winded but thanks anyway 🙂

Asked By: MoosyLager

||

Answers:

Trying to change the line of code you provided into something that works, I got this:

(lambda x, y: [2*x[i]*y[i]/GRAVITY for i in range(len(x))])(get_angles(), get_speeds())

This trick uses a lambda function to "store" the x, y values and use them in the same line.

I’m not sure this does exactly what you want, but this lambda function trick still applies.

Answered By: Ender_The_Xenocide
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.