图案设计程序无法正常工作

Pattern Design Program not working correctly

我有两个代码可以一起使用 import turtle 绘制图案。我不知道代码是否正常工作或者它是否 100% 正确,因为我遇到了多个我不知道如何修复的错误。我试过修复它们,但它们不断出现。我的程序应该根据用户的输入绘制图案。我收到缩进错误,我尝试修复它们,但它仍然无法正常工作。请帮忙!

代码文件 1:

import pattern as p

def main():
    p.setup()

playAgain = True
while playAgain:
    print("Choose a mode")
    print("1) Rectangle Pattern")
    print("2) Circle Pattern")
    print("3) Super Pattern")
mode = eval(input("Which mode do you want to play? 1, 2 or 3: "))

if mode == 1:
    width = input("Enter width for the rectangles: ")
    height = input("Enter height for the rectangles: ")
    count = input("Enter how many rectangles you'd like in the pattern: ")
    centerX, centerY = eval(input("Enter center position (x, y): "))
p.drawRectanglePattern(centerX, centerY, p.drawRectanglePattern.offset, width, height, count, p.drawRectanglePattern.rotations)

elif mode == 2:
    width = input("Enter width for the circles: ")
    height = input("Enter height for the circles: ")
    count = input("Enter how many circles you'd like in the pattern: ")
    centerX, centerY = eval(input("Enter center position (x, y): "))
p.drawCirclePattern(centerX, centerY, offset, radius, count)


elif mode == 3:
    width = input("Enter width for the super pattern: ")
    height = input("Enter height for the super pattern: ")
    count = input("Enter how many super patterns you'd like in the pattern: ")
    centerX, centerY = eval(input("Enter center position (x, y): "))

if num == "":
p.drawSuperPattern()
else:
p.drawSuperPattern(eval(num))


print("Do you want to play again?")
print("1) Yes, and keep drawings")
print("2) Yes, and clear drawings")
print("3) No, I am all done")
response = eval(input("Choose 1, 2, or 3: "))

if response == 1:
        playAgain = True
    if response == 2:
        pattern.reset()
        playAgain = True
    else:
        playAgain = False
        print("Thanks for playing!")
        pattern.done()

main()

已调用文件 2 pattern.py

import turtle
import random

SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 800

def setup():
turtle.screensize(SCREEN_WIDTH, SCREEN_HEIGHT)
turtle.speed(0)

def setRandomColor():
colors = ["misty rose", "lavender", "blue", "gold", "forest green", "orange"]
random.choice(colors)


def drawRectangle(centerX, centerY, width, height):
turtle.pd()
turtle.color((setRandomColor()))
for b in range(2):
turtle.forward(height)
turtle.left(90)
turtle.forward(width)
turtle.left(90)

def drawRectanglePattern(centerX, centerY, offset, width, height, count, rotation):
offset = 0
for c in range(count):
rotation = (360/count)
offset += 1
nextRect = (rotation/(height/2) + offset)
turtle.pu()
turtle.goto(nextRect)
drawRectangle(centerX, centerY, width, height)

你的代码是一场灾难。缩进在大多数情况下是不存在的。您调用不存在的函数。您传递忽略的参数。您以一个名称导入第二个文件,但以另一个名称调用它。您在循环内重复固定计算。等等

这似乎是一个没有测试任何东西就写了太多代码然后希望它能工作的例子。写小块,测试小块,多写小块测试等等

这里重写了您的代码以使其基本正常运行:

pattern.py 图书馆:

import turtle
from random import choice

SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 800

COLORS = ["misty rose", "lavender", "blue", "gold", "forest green", "orange"]

def setup():
    turtle.screensize(SCREEN_WIDTH, SCREEN_HEIGHT)
    turtle.speed('fastest')

def reset():
    turtle.clearscreen()

def getRandomColor():
    return choice(COLORS)

def drawRectangle(centerX, centerY, width, height):
    turtle.goto(centerX - width/2, centerY - height/2)
    turtle.pd()
    turtle.color(getRandomColor())

    for _ in range(2):
        turtle.forward(width)
        turtle.left(90)
        turtle.forward(height)
        turtle.left(90)

def drawRectanglePattern(centerX, centerY, width, height, count):
    rotation = 360 / count

    for _ in range(count):
        turtle.pu()
        turtle.left(rotation)
        drawRectangle(centerX, centerY, width, height)

def drawCirclePattern(centerX, centerY, radius, count):
    pass

def drawSuperPattern(centerX, centerY, width, height, count):
    pass

调用库的主程序:

import pattern

def main():

    while True:
        print("Choose a mode")
        print("1) Rectangle Pattern")
        print("2) Circle Pattern")
        print("3) Super Pattern")

        mode = int(input("Which mode do you want to play? 1, 2 or 3: "))

        pattern.setup()

        if mode == 1:
            width = int(input("Enter width for the rectangles: "))
            height = int(input("Enter height for the rectangles: "))
            count = int(input("Enter how many rectangles you'd like in the pattern: "))
            centerX, centerY = eval(input("Enter center position (x, y): "))

            pattern.drawRectanglePattern(int(centerX), int(centerY), width, height, count)
        elif mode == 2:
            radius = int(input("Enter the radius for the circles: "))
            count = int(input("Enter how many circles you'd like in the pattern: "))
            centerX, centerY = eval(input("Enter center position (x, y): "))

            pattern.drawCirclePattern(int(centerX), int(centerY), radius, count)
        elif mode == 3:
            width = int(input("Enter width for the super pattern: "))
            height = int(input("Enter height for the super pattern: "))
            count = int(input("Enter how many super patterns you'd like in the pattern: "))
            centerX, centerY = eval(input("Enter center position (x, y): "))

            pattern.drawSuperPattern(int(centerX), int(centerY), width, height, count)

        print("Do you want to play again?")
        print("1) Yes, and keep drawings")
        print("2) Yes, and clear drawings")
        print("3) No, I am all done")

        response = int(input("Choose 1, 2, or 3: "))

        if response == 1:
            pass
        elif response == 2:
            pattern.reset()
        else:
            print("Thanks for playing!")
            break

main()

除了填补缺失的函数之外,您还应该解决的一个主要问题是让您的程序摆脱 eval() 调用。这不是您应该使用的例程,尽管它可能提供任何便利。

用法示例

> python3 test.py
Choose a mode
1) Rectangle Pattern
2) Circle Pattern
3) Super Pattern
Which mode do you want to play? 1, 2 or 3: 1
Enter width for the rectangles: 150
Enter height for the rectangles: 275
Enter how many rectangles you'd like in the pattern: 17
Enter center position (x, y): (25, 75)
Do you want to play again?
1) Yes, and keep drawings
2) Yes, and clear drawings
3) No, I am all done
Choose 1, 2, or 3: 3
Thanks for playing!
> 

示例输出