我正在学习 python 中的基本 for 循环,并注意到变量“x”与变量“sides”相比可以正常工作。怎么来的?
我用谷歌搜索了循环,并了解了 range 和 xrange 之间的区别,但似乎与我的问题无关。以下显示了第一段有错误的代码:
ZeroDivisionError:在线整数除法或模数为零...
# This code leads to the ZeroDivisionError
import turtle
wn = turtle.Screen()
mikey = turtle.Turtle()
sides = int(input("How many sides would you like your regular
polygon to have?"))
length = int(input("How long would you like the sides to be?"))
color = ("What color would you like to fill the polygon?")
for sides in range(sides):
mikey.down()
mikey.forward(length)
mikey.left(360/sides)
# this code works fine
import turtle
wn = turtle.Screen()
mikey = turtle.Turtle()
sides = int(input("How many sides would you like your regular
polygon to have?"))
length = int(input("How long would you like the sides to be?"))
color = ("What color would you like to fill the polygon?")
x = sides
for sides in range(sides):
mikey.down()
mikey.forward(length)
mikey.left(360/x)
为什么后者可以正常工作,而前者却不行?
POPMUISE
Qyouu
相关分类