我正在尝试使Python程序连续运行,并用乌龟图形显示正确的答案。程序运行时,每次成功运行后,都会给我一个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
函数,但无济于事。
{{1}}
答案 0 :(得分:0)
我看到两个问题:控制台窗口和乌龟窗口之间有碰撞;在乌龟基于事件的世界中表现不佳。我们可以通过使用Python 3 turtle的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()
看看这是否能为您提供所需的互动。