# Terminates on first run

I am attempting to make a red circle using turtle that displays a number in the center the represents the percentage for failing, and with each click the number grows. Thus the chance for failing grows, but each time I run the program it displays the end text after I reach the number 1 each time.

``````from turtle import*
import turtle
import random
count = 0
rando = 0
def click(x, y):
global count
if -100 <= x <= 100 and -100 <= y <= 100:
count += 1
turtle.clear()
circle(count)
def circle(number):
turtle.tracer(0)
turtle.hideturtle()
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.dot(200, 'Red')
turtle.penup()
turtle.goto(0, -20)
turtle.color('Black')
turtle.write(number, align = 'center', font = ('Ink Free', 30))
turtle.update()
turtle.tracer(1)
turtle.speed(0)
turtle.Screen().onclick(click)
while rando >= count:
circle(count)
rando = random.randint(1,100)
if rando < count+1:
turtle.goto(0, -150)
turtle.write('You Lost!', align = 'center', font = ('Ink Free', 30))
turtle.onscreenclick(None)
turtle.mainloop()
``````

At first `rando == count` so your loop is behaving like a never-ending loop. Because `random.randint(1,100)` never returns 0, the test `rando >= count` always returns True.

But when you click, this code:

sets `count` to 1. And this means your test `if rando < count+1:` has a chance to succeed.

because the while loop is still running independently of your `on click` handler it means that the first moment when `count` is greater than zero the ending code `if rando < count+1` will finally run and ends your program.

1 Like

When `count` is `0`, then `count+1` is `1`. Then the loop is changing `rando` quickly and often it is equal to `1`. Then it enters the IF and prints the â€śYou lostâ€ť message.

I suspect the intention of your program was really to put the line `rando = random.randint(1,100)` outside of the loop

``````# ...
rando = random.randint(1,100)
while rando >= count:
# ...
``````

and perhaps break out of the loop after entering the IF

``````# ...
if rando < count+1:
print(f'IF {rando=}, {count=}')
turtle.goto(0, -150)
turtle.write('You Lost!', align = 'center', font = ('Ink Free', 30))
turtle.onscreenclick(None)
break
#...
``````

My code jumps to the number instead of waiting upon the user to click

``````from turtle import*
import turtle
import random
count = 0
rando = 0
def click(x, y):
global count
if -100 <= x <= 100 and -100 <= y <= 100:
count += 1
turtle.clear()
circle(count)
def circle(number):
turtle.tracer(0)
turtle.hideturtle()
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.dot(200, 'Red')
turtle.penup()
turtle.goto(0, -20)
turtle.color('Black')
turtle.write(number, align = 'center', font = ('Ink Free', 30))
turtle.update()
turtle.tracer(1)
turtle.speed(0)
turtle.Screen().onclick(click)
while rando >= count:
circle(count)
count += 1
rando = random.randint(1,100)
if rando < count+1:
turtle.goto(0, -150)
turtle.write('You Lost!', align = 'center', font = ('Ink Free', 30))
turtle.onscreenclick(None)
turtle.mainloop()

``````

I am attempting to make a red circle using turtle that displays a number in the center the represents the percentage for failing, and with each click the number grows. Thus the chance for failing grows, but each time I run the program it displays the end text after I reach the number 1 each time.

``````from turtle import*
import turtle
import random
count = 0
def click(x, y):
global count
if -100 <= x <= 100 and -100 <= y <= 100:
count += 1
turtle.clear()
circle(count)
def circle(number):
turtle.tracer(0)
turtle.hideturtle()
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.dot(200, 'Red')
turtle.penup()
turtle.goto(0, -20)
turtle.color('Black')
turtle.write(number, align = 'center', font = ('Ink Free', 30))
turtle.update()
turtle.tracer(1)
turtle.speed(0)
turtle.Screen().onclick(click)
while True:
if random.randint(1, 100) <= count:
turtle.goto(0, -150)
turtle.write('You Lost!', align='center', font=('Ink Free', 30))
turtle.onscreenclick(None)
break
circle(count)
turtle.mainloop()

``````

youâ€™re looping continuously with many iterations per second.
Since every iteration generates a new random integer, you will certainly hit a number > count in a second or less.
You should generate a new random number only with each click

Thank you, but I moved the random number generator into the click function and now it terminates after 0.

probably you have initialized with zero the variable containing the random number.
You need to initialize it with a number greater than 100, say 1000, to make sure the comparison is false until the first click

I think what you want is the code inside `while` to run after the user clicks.

If it is like so, then:

• move the code inside your `while` loop inside the `click(...)` function, so it does the check only after user clicks
• forget about `while True` because right now it blocks the `turtle.mainloop()` from executing. You donâ€™t need that loop as `turtle.mainloop()` as the name suggests contains its own loop