Python - 简单的乌龟程序
Python - Simple turtle program
我正在尝试制作一个程序,当 运行 时,在 turtle 中创建一条线、正方形或三角形。
我已经为每个形状创建了程序和 if 语句。我不知道是什么阻止了我的 turtle 程序使用输入的长度。
我的程序:
def Makeashape():
color = input('What color?')
width = input('How wide?')
size = input('How long?')
shape = input('What shape would you like to make?')
import turtle
t = turtle.Turtle()
s = turtle.Screen()
def line(width, size, color):
t.color(color)
t.width(width)
t.fd(size)
def triangle(width, size, color):
for i in range(3):
t.color(color)
t.width(width)
t.fd(size)
t.rt(120)
def square(width, size, color):
for i in range(4):
t.color(color)
t.width(width)
t.fd(size)
t.rt(90)
if shape == 'triangle':
triangle(width, size, color)
elif shape == 'square':
square(width, size, color)
elif shape == 'line':
line(width, size, color)
Makeashape()
(1) 请记住使用打印语句跟踪您的数据和控制流。在处理输入时打印输入。在您进入每个例程时打印一条有用的消息。
(2) 我认为您的实际问题很可能是您的输入仍然是字符串形式,而海龟例程需要整数或浮点数。尝试
size = int(input('How long?'))
等等
我正在尝试制作一个程序,当 运行 时,在 turtle 中创建一条线、正方形或三角形。
我已经为每个形状创建了程序和 if 语句。我不知道是什么阻止了我的 turtle 程序使用输入的长度。
我的程序:
def Makeashape():
color = input('What color?')
width = input('How wide?')
size = input('How long?')
shape = input('What shape would you like to make?')
import turtle
t = turtle.Turtle()
s = turtle.Screen()
def line(width, size, color):
t.color(color)
t.width(width)
t.fd(size)
def triangle(width, size, color):
for i in range(3):
t.color(color)
t.width(width)
t.fd(size)
t.rt(120)
def square(width, size, color):
for i in range(4):
t.color(color)
t.width(width)
t.fd(size)
t.rt(90)
if shape == 'triangle':
triangle(width, size, color)
elif shape == 'square':
square(width, size, color)
elif shape == 'line':
line(width, size, color)
Makeashape()
(1) 请记住使用打印语句跟踪您的数据和控制流。在处理输入时打印输入。在您进入每个例程时打印一条有用的消息。
(2) 我认为您的实际问题很可能是您的输入仍然是字符串形式,而海龟例程需要整数或浮点数。尝试
size = int(input('How long?'))
等等