我正在尝试让Python程序连续运行,并使用海龟图形显示正确答案。虽然程序工作正常,但每次成功运行后,它都会给我一个turtle.Terminator错误。也就是说,它将在第一次运行时出现错误turtle.Terminator,然后在第二次运行时运行正常,没有错误。
我尝试使用from turtle import Turtle,并相应地更改变量,但无济于事。我也尝试过创建一个创建窗口的函数,然后调用result()函数,但也无济于事。
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发布于 2019-06-22 14:02:00
我看到了两个问题:在控制台窗口和turtle窗口之间徘徊;在turtle的基于事件的世界中玩得不好。我们可以通过使用Python3Turtle的numinput()和textinput()方法,而不是input(),并添加计时器事件来解决这两个问题:
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()看看这是否能给你提供你想要的交互。
https://stackoverflow.com/questions/56712716
复制相似问题