0

I am trying to write program with turtle python that ask the user for a number and let him click on the screen the same number of times.

import turtle

t = turtle.Turtle()
count = 0

def up_count(x,y):
  global count
  count = count + 1
  print count
  return

def start():

  num1=int(raw_input("enter number"))
  print "in the gaeme you need to enter number and click on button",num1,"times"
  s = t.getscreen()
  if not num1 == count:
    s.onclick(up_count)
  else:
    t.mainloop()


start()

The problem is that I can not get out of mainloop when num1 == count.

How can I get out of mainloop?

I use https://repl.it/@eliadchoen/BrightShinyKernel for the program.

cdlane
  • 40,441
  • 5
  • 32
  • 81
gal leshem
  • 551
  • 1
  • 6
  • 18
  • 1
    Minor: you don't need `global count` in the second function, because it doesn't change this global variable. – goodvibration Jun 03 '19 at 08:51
  • Acc to the docs [mainloop](https://docs.python.org/3.1/library/turtle.html#turtle.mainloop) *starts* the eventloop rather than getting out of it. You can check out [How to close a python turtle window](https://stackoverflow.com/questions/36826570/how-to-close-the-python-turtle-window-after-it-does-its-code) – shriakhilc Jun 03 '19 at 09:05

1 Answers1

0

You don't get out of mainloop() until you're done with your use of turtle graphics. For example:

from turtle import Screen, Turtle, mainloop

count = 0
number = -1

def up_count(x, y):
    global count
    count += 1

    if number == count:
        screen.bye()

def start():
    global number

    number = int(raw_input("Enter number: "))
    print "You need to click on window", number, "times"

    screen.onclick(up_count)

screen = Screen()
turtle = Turtle()

start()

mainloop()

print "Game over!"

My guess is this isn't your goal, so explain what it is you want to happen, in your question.

cdlane
  • 40,441
  • 5
  • 32
  • 81