所以,首先,这里有要求:
到目前为止,这是我的代码:
import turtle
turtle = turtle.Screen()
def circle():
def triangle():
def square():
def pentagon():
def hexagon():
def heptagon():
for list in ["1.Circle","2.Triangle","3.Square","4.Pentagon","5.Hexagon","6.Heptagon"]:
print(list)
shape1 = input("Choose one number from the following:")
if shape1 == "1":
for list in ["2.Triangle","3.Square","4.Pentagon","5.Hexagon","6.Heptagon"]:
print(list)
shape2 = input("Choose one number from the following:")
if shape2 == "2":
elif shape2 == "3":
elif shape2 == "4":
elif shape2 == "5":
elif shape2 == "6":
else:
print("Incorrect input. Please try again.")
if shape1 == "2":
if shape1 == "3":
if shape1 == "4":
if shape1 == "5":
if shape1 == "6":
else:
print("Incorrect input. Please try again.")
基本上,我非常困惑。我可以找到在用户选择的一行中绘制三个形状的唯一方法是做每一个可能的结果 - 123,124,125,126,132,134 ......等等,这将永远,看起来很可怕,然后我每次都要编写海龟命令。正如你所看到的,我尝试使用def,但是在我较小的测试代码中它根本不起作用,所以我也不确定我是否正确理解它。
除了所有这些之外,我如何确保所有形状或它们应该在哪里?我能看到它的唯一方法是用不同的" goto" s为每个结果编写单独的代码。
有没有办法让用户一次性放置所有三个选项(" 123"," 231"等)然后让程序遍历每个数字并绘制反过来呢?有没有办法将每个数字分配给绘制形状的一组代码?我对这一切都很陌生。我感谢你能给我的任何帮助。谢谢!
答案 0 :(得分:0)
下面是一个示例框架,它提示用户(缩小)形状列表,划分画布并绘制它们。它只实现了圆圈,你需要填写其他形状,并且它远离完成的代码,你需要添加错误检查和其他最后的润色:
import turtle
CANVAS_WIDTH = 900
CANVAS_HEIGHT = 600
CHROME_WIDTH = 30 # allow for window borders, title bar, etc.
SHAPE_CHOICES = 3
def circle(bounds):
turtle.penup()
center_x = bounds['x'] + bounds['width'] // 2
bottom_y = bounds['y']
turtle.setposition(center_x, bottom_y)
turtle.pendown()
turtle.circle(min(bounds['width'], bounds['height']) // 2)
def triangle(bounds):
circle(bounds)
def square(bounds):
circle(bounds)
def pentagon(bounds):
circle(bounds)
def hexagon(bounds):
circle(bounds)
def heptagon(bounds):
circle(bounds)
DESCRIPTION, FUNCTION = 0, 1
shapes = [("Circle", circle), ("Triangle", triangle), ("Square", square), ("Hexagon", hexagon), ("Heptagon", heptagon)]
choices = []
turtle.setup(CANVAS_WIDTH + CHROME_WIDTH, CANVAS_HEIGHT + CHROME_WIDTH)
for _ in range(SHAPE_CHOICES):
for i, (description, function) in enumerate(shapes):
print("{}. {}".format(i + 1, description))
choice = int(input("Choose one number from the above: ")) - 1
choices.append(shapes[choice][FUNCTION])
del shapes[choice]
x, y = -CANVAS_WIDTH // 2, -CANVAS_HEIGHT // 2
width, height = CANVAS_WIDTH // SHAPE_CHOICES, CANVAS_HEIGHT // SHAPE_CHOICES
# I'm dividing the screen into thirds both horizontally and vertically
bounds = dict(x=x, y=y, width=width, height=height)
for choice in choices:
choice(bounds)
bounds['x'] += width
bounds['y'] += height
turtle.done()