【问题标题】:How to solve Index out of range for complex code如何解决复杂代码的索引超出范围
【发布时间】:2020-02-22 04:14:53
【问题描述】:

我有一个代码询问用户是否希望程序绘制矩形图案(如果他们输入数字 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)

【问题讨论】:

  • 您是否尝试将您的elif 语句更改为仅if。如果您不选择第一个选项,else/if 将使用 else
  • 请将帖子中的代码减少到演示问题所需的绝对最小值。

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


【解决方案1】:

您可能希望将绘制随机矩形/圆形的代码提取到单独的函数中,而无需任何参数。然后你可以按照你最初的计划随机调用它们

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

【讨论】:

    猜你喜欢
    • 2020-06-20
    • 2014-07-11
    • 2021-10-01
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多