如何解决索引超出范围的复杂代码

时间:2019-10-25 19:42:46

标签: python python-3.x algorithm random refactoring

我有一个代码,询问用户是否要程序绘制矩形图案(如果输入数字1)或圆形图案(如果输入数字2)。

它可以正常工作,但是如果用户输入数字3,则程序必须随机选择在屏幕上的任意位置绘制圆形或矩形图案。

选择(3)之后,用户唯一需要输入的就是在屏幕上绘制多少个随机图案。

问题是我遇到了错误

  

“第91行,在drawSuperPattern中rand_shape = shape_functs [randint(0,1)]   IndexError:列表索引超出范围”。

我知道它尝试访问元素0,rand_shape将设置为“ drawRectanglePattern”,但是元素1上没有对象。

我该如何解决?我曾尝试修复它,但似乎无法解决。

如果我将其更改为: shape_functs = [drawRectanglePattern, drawCirclePattern]该代码将无效,因为它们采用了不同数量的参数。

主模块:

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: "))
            offset = int(input("Enter the offset for the rectangle: "))
            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, offset)
        elif mode == 2:
            radius = int(input("Enter the radius for the circles: "))
            offset2 = int(input("Enter the offset for the circle: "))
            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, offset2)
        elif mode == 3:
            super = int(input("Enter how many super patterns you'd like: "))


            pattern.drawSuperPattern(super)

        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()

pattern.py代码:

import turtle
from random import choice, randint

turtle.speed('fastest')

SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 800

COLORS = ["pink", "red", "purple", "black", "dark cyan", "lavender", "blue", "yellow", "dark green", "orange", "gold", "brown", "tan"]


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, offset):
    rotation = 360 / count
    turtle.pu()
    turtle.goto(centerX, centerY)

    for _ in range(count):

        turtle.pu()

        turtle.forward(offset)
        turtle.right(90+rotation/2)

        drawRectangle(centerX, centerY, width, height)

        turtle.pu()

        turtle.left(90+rotation/2)
        turtle.backward(offset)


        turtle.right(rotation)

def drawCircle(centerX, centerY, radius):

    turtle.pd()
    turtle.color(getRandomColor())
    turtle.circle(radius)


def drawCirclePattern(centerX, centerY, radius, count, offset2):
    rotation = 360 / count
    turtle.pu()
    turtle.goto(centerX, centerY)

    for _ in range(count):

        turtle.pu()

        turtle.forward(offset2)
        turtle.right(90+rotation/2)

        drawCircle(centerX, centerY, radius)


        turtle.pu()

        turtle.left(90+rotation/2)
        turtle.backward(offset2)


        turtle.right(rotation)

def drawSuperPattern(super):

    for i in range(super):
        shape_functs = [drawRectanglePattern]

        rand_shape = shape_functs[randint(0, 1)]

        width = randint(0, 100)
        height = randint(0, 100)
        offset = randint(0, 100)
        count = randint(0, 100)
        centerX = randint(0, 100)
        centerY = randint(0, 100)

        rand_shape(int(centerX), int(centerY), width, height, count, offset)

        shape_funcs = [drawCirclePattern]
        rand_shape2 = shape_funcs[randint(0, 1)]

        offset2 = randint(0, 100)
        count = randint(0, 100)
        radius = randint(0, 100)
        centerX = randint(0, 200)
        centerY = randint(0, 200)

        rand_shape2(centerX, centerY, count, offset2, radius)

1 个答案:

答案 0 :(得分:2)

您可能希望提取将随机矩形/圆形绘制成不带任何参数的单独函数的代码。然后,您可以按照原计划随机调用它们

def drawRandomRectanglePattern():
    drawRectanglePattern(randint(0, 100), ..., randint(0, 100))

def drawRandomCirclePattern():
    drawCirclePattern(randint(0, 100), ..., randint(0, 100))

然后使用它们

def drawSuperPattern(super):
    shape_functs = [drawRandomRectanglePattern, drawRandomCirclePattern]
    for i in range(super):
        rand_shape = shape_functs[randint(0, len(shape_functs) - 1)]
        rand_shape()