Divide a Python Turtle program into subprograms - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Divide a Python Turtle program into subprograms (/thread-25141.html) |
Divide a Python Turtle program into subprograms - TurtleOHG - Mar-21-2020 Hello everyone, I want to draw a simple house with several windows and a door by using the python turtle inferface. The window(s) and door(s) have to be implemented by modular programming. In principle I am successful in it (see program code below). import turtle def contour(): turtle.left(90) turtle.forward(200) turtle.right(45) turtle.forward(142) turtle.right(90) turtle.forward(142) turtle.right(45) turtle.forward(200) turtle.right(90) turtle.forward(200) def window(): turtle.seth(90) for a in range (4): turtle.forward(30) turtle.right(90) def door(): turtle.seth(90) turtle.forward(60) turtle.right(90) turtle.forward(30) turtle.right(90) turtle.forward(60) turtle.right(90) turtle.forward(30) contour() turtle.pu() turtle.goto(25,150) turtle.pd() window() turtle.pu() turtle.goto(85,150) turtle.pd() window() turtle.pu() turtle.goto(85,0) turtle.pd() door() turtle.done()But instead of defining CONTOUR, WINDOW and DOOR at the beginning of the main program HOUSE.py, I would like to have the contour, windows and doors as seperate independent programs CONTOUR.py, DOOR.py and WINDOW.py and then "import" them in the main program HOUSE.py. So something like this: import turtle contour.py #Which command is needed to execute the program "contour.py" turtle.pu() turtle.goto(25,150) turtle.pd() window.py turtle.pu() turtle.goto(85,150) turtle.pd() window.py turtle.pu() turtle.goto(85,0) turtle.pd() door.py turtle.done()I have tried to solve the problem with the command import [NAME OF THE PROGRAM] but then I have to create a new turtle in each program. This then results in several turtles with its own different positions, which is not wanted, since modularity is not given than any more. So in the end I do want to create modularity by using seperate programs (*py) rather than using seperate def-commands within the mnain program. I hope somebody can help me out Waiting for your response RE: Divide a Python Turtle program into subprograms - buran - Mar-21-2020 pass the turtle as argument to your functions. then you can have one turtle in the main file and pass it when calling the imported functions. also you need to import the other scripts, e.g. import countour or from contour import contour , not just write contour.py
RE: Divide a Python Turtle program into subprograms - TurtleOHG - Mar-21-2020 How exactly does that work? I am just starting to learn the Python language. Would you be able to provide me with an example or could you perhaps rewrite my program in a way it works with your proposal? RE: Divide a Python Turtle program into subprograms - buran - Mar-21-2020 for example, in contour.py you will have def contour(turtle): turtle.left(90) turtle.forward(200) turtle.right(45) turtle.forward(142) turtle.right(90) turtle.forward(142) turtle.right(45) turtle.forward(200) turtle.right(90) turtle.forward(200)then in main.py you will have import turtle from contour import contour # import contour function from contour.py contour(turtle) turtle.done()obviously it works, but if you have learned that it's better to create an instance of Turtle class, e.g. my_turtle = turtle.Turtle()and use it RE: Divide a Python Turtle program into subprograms - TurtleOHG - Mar-21-2020 WOW, Many, many thanks It now works!!! You saved my day! RE: Divide a Python Turtle program into subprograms - buran - Mar-21-2020 note that you make your functions more universal, e.g. pass optional arguments for x, y position of the turtle to start from. If no x, y are passed - used the current position, but if user pass x and y - move the turtle to that position in the beginning of the function. That will allow to use same function to draw same shape at different locations - e.g. 2 windows at different positions |