复杂代码如何解决Index out of range
How to solve Index out of range for complex code
我有一个代码询问用户是否希望程序绘制矩形图案(如果他们输入数字 1)或圆形图案(如果他们输入数字 2)。
它工作正常,但如果用户输入数字 3,则程序必须随机选择在屏幕上的随机位置绘制圆形或矩形图案。
用户在选择 (3) 后必须输入的唯一输入是应在屏幕上绘制多少个随机图案。
问题是我得到一个错误
"line 91, in drawSuperPattern rand_shape = shape_functs[randint(0,1)]
IndexError: list index out of range".
我知道它试图访问元素 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)
您可能希望将随机绘制 rectangle/circle 的代码提取到不带任何参数的单独函数中。然后就可以按原计划随机调用
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()
我有一个代码询问用户是否希望程序绘制矩形图案(如果他们输入数字 1)或圆形图案(如果他们输入数字 2)。
它工作正常,但如果用户输入数字 3,则程序必须随机选择在屏幕上的随机位置绘制圆形或矩形图案。
用户在选择 (3) 后必须输入的唯一输入是应在屏幕上绘制多少个随机图案。
问题是我得到一个错误
"line 91, in drawSuperPattern rand_shape = shape_functs[randint(0,1)] IndexError: list index out of range".
我知道它试图访问元素 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)
您可能希望将随机绘制 rectangle/circle 的代码提取到不带任何参数的单独函数中。然后就可以按原计划随机调用
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()