通过检查变量停止turtle中的主循环

2024-09-30 22:21:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用TurtlePython编写一个程序,要求用户输入一个数字,然后让他在屏幕上按相同的次数

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()

问题是当num1==count时我不能离开mainloop

我怎样才能离开主回路

我对程序使用https://repl.it/@eliadchoen/BrightShinyKernel


Tags: 用户程序numberdefcount数字startprint
1条回答
网友
1楼 · 发布于 2024-09-30 22:21:33

在你完成海龟图形的使用之前,你不会离开mainloop()。例如:

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!"

我猜这不是你的目标,所以在你的问题中解释一下你想发生什么

相关问题 更多 >