Turtle.Terminator error occurs after every other successful run

Viewed 544

I am trying to make a Python program run continuously, with turtle graphics displaying the correct answer. While the program works, after every successful run, it will give me a turtle.Terminator error. I.e it will run the first time with an error of turtle.Terminator, then the second time it will run fine with no errors.

I tried using from turtle import Turtle, and changing the variables accordingly, but to no avail. I have also tried to make a function for creating the window, and then calling the result() function, but also to no avail.

import turtle 
import random

def result(x, comment):
    width = 450
    length = 335
    turtle.setup(width,length)
    turtle.bgcolor("black")
    turtle.title("Guess the number")
    fonts = ['times new roman', 'americana', 'verdana']
    turtle.hideturtle()
    turtle.penup()
    turtle.goto(0, 40)
    turtle.color("green")
    turtle.write(x, align = "center", font = (random.choice(fonts),30, 
    "italic"))
    turtle.pendown()
    turtle.penup()
    turtle.goto(0, -40)
    turtle.color("blue")
    turtle.write(comment, align = "center", font = (random.choice(fonts),30, 
    "italic"))
    turtle.pendown()
    #turtle.done()

p = 'y'
while p == 'y' or p == 'Y':
    bot = random.randint(1,1)
    p1 = int(input("Please enter a number: "))
    if bot == p1:
        r = "WINNER"
        c = "GREAT JOB"
        result(r,c)
    if bot != p1:
        r = "LOSER"
        i = "you're dumb as a rock"
        result(r, i)
    p = input("do you want to try again: " )
    if p == 'y' or p == 'Y':
        turtle.clear()
    else:
        turtle.done()
        break
1 Answers

I see two issues: boucing between the console window and turtle window; not playing nicely in turtle's event-based world. We can address both issues by using Python 3 turtle's numinput() and textinput() methods, instead of input(), and adding timer events:

from turtle import Screen, Turtle
from random import choice, randint

WIDTH, HEIGHT = 640, 480
MINIMUM, MAXIMUM = 1, 10
TITLE = "Guess the number"
FONTS = ['times new roman', 'americana', 'verdana']

def result(status, comment):

    turtle.goto(0, 40)
    turtle.color("green")
    turtle.write(status, align="center", font=(choice(FONTS), 30, "italic"))

    turtle.goto(0, -40)
    turtle.color("blue")
    turtle.write(comment, align="center", font=(choice(FONTS), 30, "italic"))

def play():
    turtle.clear()

    number = randint(MINIMUM, MAXIMUM)

    answer = screen.numinput(TITLE, "Please enter a number:", minval=MINIMUM, maxval=MAXIMUM)

    if number == answer:
        result("WINNER", "GREAT JOB")
    else:
        result("LOSER", "you're dumb as a rock")

    answer = screen.textinput(TITLE, "Do you want to try again?")

    screen.ontimer(play if answer in {'y', 'Y'} else screen.bye, 250)

screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.bgcolor("black")
screen.title(TITLE)

turtle = Turtle()
turtle.hideturtle()
turtle.penup()

play()

screen.mainloop()  # returns upon screen.bye()

See if this gives you the interaction you're looking for.

Related