0

I am working on a Python digital clock using the turtle module. However, it is throwing an error at line 41, column 12. Can someone help me identify and fix the issue?

I tried whatever can be done by my side but it seems not to be satisfying. Help me and there is a one more error saying syntax error at the font.

import turtle as tl
import time 
import datetime as dt

#create a turtle to display time
t = tl.Turtle()

#create a turtle to create a rectangular box
t1 = tl.Turtle()

#create screen
s = tl.Screen()
s.bgcolor('black') #set background colour

sec = dt.datetime.now().second
min = dt.datetime.now().minute
hour = dt.datetime.now().hour

t1.pensize(3)
t1.color('white')
t1.penup()

# set the position of the turtle
t1.goto(-20, 0)
t1.pendown()

#create rectangular box
for r in range(2):
    t1.forward(200)
    t1.left(90)
    t1.forward(70)
    t1.left(90)

# hide the turtle
t1.hideturtle()

while True:
    t.hideturtle()
    t.clear()
    #display the time
    t.write(str(hour).zfill(2)
        + ":" + str(min).zfill(2) 
        + ":" + str(sec).zfill(2),
        font=('Arial Narrow', 35, 'bold'))

    time.sleep(1)
    sec += 1
    
    if sec == 60:
        sec = 0
        min += 1
    
    if min == 60:
        min = 0 
        hour += 1
        
    if hour == 12:
        hour = 0 
quamrana
  • 37,849
  • 12
  • 53
  • 71

1 Answers1

0

This code ran fine for me. The only possibly objectionable thing on the lines indicated is lack of continuation characters:

t.write(str(hour).zfill(2) \
    + ":" + str(min).zfill(2) \
    + ":" + str(sec).zfill(2), \
    font=('Arial Narrow', 35, 'bold'))

A real issue was the font color and background color are the same so you can't see the time. Below is my rework of your code:

from turtle import Screen, Turtle
from datetime import datetime
from time import sleep

# create screen
screen = Screen()
screen.bgcolor('black')  # set background colour

# create a turtle to create a rectangular box
box = Turtle()
box.pensize(3)
box.color('white')

# set the position of the turtle
box.penup()
box.setx(-20)
box.pendown()

# create rectangular box
for _ in range(2):
    box.forward(200)
    box.left(90)
    box.forward(70)
    box.left(90)

# hide the turtle
box.hideturtle()

second = datetime.now().second
minute = datetime.now().minute
hour = datetime.now().hour

# create a turtle to display time
turtle = Turtle()
turtle.hideturtle()
turtle.color('white')

while True:
    turtle.clear()

    # display the time
    turtle.write(str(hour).zfill(2) \
        + ":" + str(minute).zfill(2) \
        + ":" + str(second).zfill(2), \
        font=('Arial Narrow', 35, 'bold'))

    sleep(1)

    second += 1

    if second == 60:
        second = 0
        minute += 1

    if minute == 60:
        minute = 0
        hour += 1

    if hour == 12:
        hour = 0

We shouldn't be using while True: and sleep(1) but rather encapsulate the code into a function and use the ontimer(1000) method of the screen.

cdlane
  • 40,441
  • 5
  • 32
  • 81