【发布时间】:2025-11-13 13:50:09
【问题描述】:
我不断收到一条错误消息,指出“i”是一个未使用的变量,但我正在使用它来迭代一个循环,该循环具有取决于用户输入 (numShapes) 的变量范围。当它是一个整数值时,为什么python不接受该变量?
***此代码用于调用 turtle 模块的两个绘图函数之一,随机放置可变数量 (numShapes) 的不同大小和位置的绘图。
import random
import turtle, BoundingBox
from TurtleShapes import drawOneSquare
from TurtleShapes import drawOneShape
x = turtle.Turtle()
def drawEverywhere(x, func):
numShapes = int(input("How many shapes?"))
for i in range(numShapes):
x.penup()
x.goto((random.randint(-1150,1150), random.randint(-550,550))
for i in range(numShapes):
func(turtle, size))
size = random.randint(10,40)
if __name__ == '__main__':
win = turtle.Screen()
BoundingBox.drawBoundingBox()
### Decide which shape ###
input("Which shape? 's' for square or 'c' for circle")
if input == 's':
drawEverywhere(turtle, drawOneSquare)
elif input == 'c':
drawEverywhere(turtle, drawOneShape)
else:
print('invalid input')
win.exitonclick()
【问题讨论】:
标签: loops iteration turtle-graphics