Search code examples
pythonturtle-graphicspython-turtle

Why does turtle keep saying "an exception has been raised"?


Using turtle I created a map function:

def loadStreetOne(direction):
    wn = turtle.Screen()
    wn.setup(height = 300, startx = 0, starty = -1)
    turtle.title("Map")
    turtle.bgcolor("white")
    player = turtle.Turtle()
    drawer = turtle.Turtle()
    player.color("red")
    drawer.hideturtle()
    drawer.penup()
    drawer.goto(-312,-100)
    player.penup()
    if direction == "left" or direction == "Left": player.goto(-300,0)
    elif direction == "right" or direction == "Right":
        player.goto(300,4)
        player.lt(90)
    drawer.pendown()
    drawer.color("blue")
    drawer.goto(307,-100)
    drawer.penup()
    drawer.goto(-312,100)
    drawer.pendown()
    drawer.goto(307,100)
    turtle.color('black')
    style = ('Courier', 30)
    turtle.write('Street', font=style, align='center')
    turtle.hideturtle()
    npc1 = turtle.Turtle()
    npc2 = turtle.Turtle()
    npc1.penup()
    npc2.penup()
    npc1.goto(-120,100)
    npc1.rt(90)
    npc2.goto(120,100)
    npc2.rt(90)
    return player, npc1, npc2

When the player finished interacting, I used this function to make the character go to the other side and close the turtle window afterwards:

def arriveOtherSide(player, direction):
    player.speed(2)
    if direction == "left": player.goto(300, 0)
    elif direction == "right": player.goto(-300, 0)
    time.sleep(1)
    turtle.bye()

When I first called it everything was fine:

player, npc1, npc2 = loadStreetOne("left")

I did the same thing for the second function, which also worked fine:

arriveOtherSide(player, "left")

But after I loaded the same street again, and using the exact same line to call the function again, it simply returned an error:

Traceback (most recent call last):
  File "C:\Users\surui\OneDrive\Desktop\Study\Computer Engineering\Graded Work\Rightfully\Rightfully\setup.py", line 50, in <module>
    player, npc1, npc2 = loadStreetOne("left")
  File "C:\Users\surui\OneDrive\Desktop\Study\Computer Engineering\Graded Work\Rightfully\Rightfully\setup.py", line 8, in loadStreetOne
    player = turtle.Turtle()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\turtle.py", line 3816, in __init__
    visible=visible)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\turtle.py", line 2557, in __init__
    self._update()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\turtle.py", line 2660, in _update
    self._update_data()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\turtle.py", line 2646, in _update_data
    self.screen._incrementudc()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\turtle.py", line 1292, in _incrementudc
    raise Terminator
turtle.Terminator

My program looks like this:

import turtle
import time
def loadStreetOne(direction):
    wn = turtle.Screen()
    wn.setup(height = 300, startx = 0, starty = -1)
    turtle.title("Map")
    turtle.bgcolor("white")
    player = turtle.Turtle()
    drawer = turtle.Turtle()
    player.color("red")
    drawer.hideturtle()
    drawer.penup()
    drawer.goto(-312,-100)
    player.penup()
    if direction == "left" or direction == "Left": player.goto(-300,0)
    elif direction == "right" or direction == "Right":
        player.goto(300,4)
        player.lt(90)
    drawer.pendown()
    drawer.color("blue")
    drawer.goto(307,-100)
    drawer.penup()
    drawer.goto(-312,100)
    drawer.pendown()
    drawer.goto(307,100)
    turtle.color('black')
    style = ('Courier', 30)
    turtle.write('Street', font=style, align='center')
    turtle.hideturtle()
    npc1 = turtle.Turtle()
    npc2 = turtle.Turtle()
    npc1.penup()
    npc2.penup()
    npc1.goto(-120,100)
    npc1.rt(90)
    npc2.goto(120,100)
    npc2.rt(90)
    return player, npc1, npc2

def arriveOtherSide(player, direction):
    player.speed(2)
    if direction == "left": player.goto(300, 0)
    elif direction == "right": player.goto(-300, 0)
    time.sleep(1)
    turtle.bye()

player, npc1, npc2 = loadStreetOne("left")
arriveOtherSide(player, "left")
time.sleep(3)
player, npc1, npc2 = loadStreetOne("left")
arriveOtherSide(player, "left")

Does anyone know why and how can I fix this?


Solution

  • turtle.bye() closes the turtle window forever. No drawing is possible after you call this function. Remove that line.