Python:indexer错误:使用randint函数列出索引超出范围
我有一个代码,询问用户是否希望程序绘制矩形图案(如果他们输入数字1)或圆形图案(如果他们输入数字2) 它工作得很好,但是如果用户输入数字3,则程序必须随机选择在屏幕上的随机位置绘制圆形或矩形图案 用户在选择(3)后必须输入的唯一输入是屏幕上应绘制多少随机图案 问题是我遇到了一个错误 “第91行,在drawSuperPattern rand_shape=shape_functs[randint(0,1)]索引器中:列表索引超出范围” 我怎样才能解决这个问题?我试着把它修好,但似乎解决不了 我的主要代码:Python:indexer错误:使用randint函数列出索引超出范围,python,python-3.x,Python,Python 3.x,我有一个代码,询问用户是否希望程序绘制矩形图案(如果他们输入数字1)或圆形图案(如果他们输入数字2) 它工作得很好,但是如果用户输入数字3,则程序必须随机选择在屏幕上的随机位置绘制圆形或矩形图案 用户在选择(3)后必须输入的唯一输入是屏幕上应绘制多少随机图案 问题是我遇到了一个错误 “第91行,在drawSuperPattern rand_shape=shape_functs[randint(0,1)]索引器中:列表索引超出范围” 我怎样才能解决这个问题?我试着把它修好,但似乎解决不了 我的主要
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)
形状函数列表中只有一项:
shape_functs = [drawRectanglePattern]
rand_shape = shape_functs[randint(0,1)]
如果尝试访问元素0,rand_shape将设置为“drawRectanglePattern”,但元素1处没有对象,它超出了列表的边界。如果您尝试访问它,它将出错
你可以试试:
shape_functs = [drawRectanglePattern, drawCirclePattern]
这将解决该错误,但由于drawRectanglePattern和drawCirclePattern使用不同数量的参数,因此当您尝试调用rand_shape时,代码将失败
这里要使用的一个好的软件模式,特别是当您有许多不同的形状可供选择时,将使用“继承”。对于您的简单用例,您可以使用以下内容:
randnum = randint(0, 1)
if randnum == 0:
# set up rectangle inputs
drawRectanglePattern(int(centerX), int(centerY), width, height, count, offset)
elif randnum == 1:
# set up circle inputs
drawCirclePattern(centerX, centerY, radius, count, offset2)
我试过了,但不起作用。我已经在pattern.py模块上编写了该代码,程序没有给我任何错误,但是程序没有绘制模式。有一个小的输入错误,应该读取randnum=randint(0,1)(不是1,2)。否则代码对我来说运行得很好。你是在主代码还是模式代码中添加的?”因为矩形输入与def drawRectanglePattern不同。此外,对于超级模式,用户只需输入必须绘制的模式数,而无需输入其他任何内容。这就是设置输入不起作用的原因。