Python Forum
Trying to make a board with turtle, nothing happens when running script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to make a board with turtle, nothing happens when running script
#1
Hello,
I'm trying to make a board with turtle ( which I'm new to ) using loops and functions, but nothing seems to work. When I run the script, nothing happens. (I use Pyzo as an IDE)
There are 2 functions : one for making the lines, and one for making the columns. There must be a loop that repeats for n times which determines the number of lines there will be. ; length is obviously the number of pixels the line will be. It's the same thing for columns . Also, between each lines / columns, there must be 20 pixels.

Here is my code :

import turtle
def lines(n,length):
    for i in range(n):
        turtle.pen(fillcolor = "blue", pencolor = "black", pensize = 3)
        turtle.forward(length)
        turtle.up()
        turtle.right(90)
        turtle.forward(20)
        turtle.left(90)
        turtle.down()
        turtle.up()
    turtle.end_fill()
    turtle.done()
    window.exitonclick()

lines(3,32)

def columns(n,length):
    for i in range(n):
        turtle.pen(fillcolor = "green", pencolor = "black", pensize = 3)
        turtle.right(90)
        turtle.forward(length)
        turtle.up()
        turtle.left(90)
        turtle.forward(20)
        turtle.right(90)
        turtle.down()
    turtle.end_fill()
    turtle.done()
    window.exitonclick()
columns(5,7)
Can someone help a beginner ?
Thanks !
Reply
#2
Some possible improvements:
- turtle up and down must be in the right sequence
- end fill should be preceded by begin fill.
- How many rows & columns are you drawing ?
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
(Oct-28-2023, 01:52 PM)DPaul Wrote: Some possible improvements:
- turtle up and down must be in the right sequence
- end fill should be preceded by begin fill.
- How many rows & columns are you drawing ?
Paul
The number of rows and columns depends on what we put instead of " n ". So for example, if I put lines(5,87) there will be 5 lines of 87 pixels each. It's the same for columns.
Reply
#4
This loop draws 1 line.
    for i in range(n):
        turtle.pen(fillcolor = "blue", pencolor = "black", pensize = 3)  # Place in front of loop.
        turtle.forward(length)
        turtle.up()
        turtle.right(90)
        turtle.forward(20)
        turtle.left(90)
        turtle.down()
        turtle.up()  # <- Pen is up when second iteration of loop runs
I would start by changing the code to this:
import turtle


def lines(n, length):
    turtle.pen(fillcolor="blue", pencolor="black", pensize=3)
    turtle.begin_fill()
    for i in range(n):
        turtle.forward(length)
        turtle.up()
        turtle.right(90)
        turtle.forward(20)
        turtle.left(90)
        turtle.down()
    turtle.end_fill()


def columns(n, length):
    turtle.pen(fillcolor="green", pencolor="black", pensize=3)
    turtle.begin_fill()
    for i in range(n):
        turtle.right(90)
        turtle.forward(length)
        turtle.up()
        turtle.left(90)
        turtle.forward(20)
        turtle.right(90)
        turtle.down()
    turtle.end_fill()


lines(3, 32)
columns(5, 7)
turtle.done()
Now you can at least see the turtle errors.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No Internet connection when running a Python script basil_555 8 663 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 522 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Make entire script run again every 45 mo NDillard 0 325 Jan-23-2024, 09:40 PM
Last Post: NDillard
  Help Running Python Script in Mac OS emojistickers 0 351 Nov-20-2023, 01:58 PM
Last Post: emojistickers
  Python script running under windows over nssm.exe JaroslavZ 0 724 May-12-2023, 09:22 AM
Last Post: JaroslavZ
  Running script with subprocess in another directory paul18fr 1 3,814 Jan-20-2023, 02:33 PM
Last Post: paul18fr
  Make console show after script was built with Pyinstaller --NOCONSOLE? H84Gabor 0 1,215 May-05-2022, 12:32 PM
Last Post: H84Gabor
Photo HOW FIX MY BOARD GAME LAZABI 3 1,481 Apr-01-2022, 04:23 PM
Last Post: BashBedlam
  Make my py script work only on 1 compter tomtom 14 3,892 Feb-20-2022, 06:19 PM
Last Post: DPaul
  The game should now run and terminate once the board is full, but still can’t identif rango 0 1,463 Jul-22-2021, 03:24 AM
Last Post: rango

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020