-1

I'm trying to convert the function below to the lambda function. And this function below has some functions that need to be executed in order. I searched many times, but there aren't have any clues. What I trying to convert is

def drawSquare(moveLength):
    A_Turtle.right(90)
    moveAndturn(moveLength)
    for n in range(6):                     
        A_Turtle.forward(moveLength)
        A_Turtle.left(90)

and this is what I do so far.

moveAndturn = lambda moveLength : [A_Turtle.forward(moveLength) A_Turtle.left(90) for n in range(6)]
  • A lambda is completely inappropriate here. What makes you think that converting this perfectly fine function into a lambda would be of any use? – jasonharper Apr 05 '21 at 15:02
  • What you did with your lambda is list comprehension, needed to generate lists, not execute code. If you **really** need to put everything in a lambda (and you don't, as stated in [PEP8](https://www.python.org/dev/peps/pep-0008/#programming-recommendations)), you can pass multiple instructions separated by semicolon – crissal Apr 05 '21 at 15:36
  • Its's just for my school project. I will try to use semicolon. Thank you for the answer :) – GG.code Apr 05 '21 at 23:02

1 Answers1

0

Before, A Disclaimer

This use of list comprehension in Python is problematic because it can hide the semantics of the code and I would advice you not to use it in this case. Your code is fine as is, but you can use lambda and list comprehensions to do the same.

draw_square = lambda moveLength : [
                    A_Turtle.right(moveLength), 
                    moveAndturn(moveLength), 
                    [
                        [A_Turtle.forward(moveLength), A_Turtle.left(moveLength)] for i in range(4)]
                    ]