。问题:实施以下伪代码以在屏幕上绘制方格旗。
1. Ask the user for the size of the checkered flag (n).
2. Draw an n x n grid to the screen.
3. For i = 0,2,4,...,62:
4. row = i // n
5. offset = row % 2
6. col = (i % n) + offset
请复制并粘贴该链接,请参阅输出:http://www.awesomescreenshot.com/image/45977/12eaef67de44c2b291ecd47fe8d10135
我实现了伪代码,但我需要一些帮助。
我能够绘制n * n网格,但之后什么也没发生。我认为我在main()
函数中做错了什么
我的节目:
from turtle import*
def size():
size = eval(input("Please enter the size of the checkered flag: "))
return size
def draw(n):
wn = Screen()
wn.setworldcoordinates(-1,-1,10,10)
pen = Turtle()
for i in range(0,n+1):
pen.up()
pen.goto(0,i)
pen.down()
pen.forward(n)
pen.left(90)
for i in range(0,n+1):
pen.up()
pen.goto(i,0)
pen.down()
pen.forward(n)
def findGrid(n):
for i in range(0,63):
row = i // n
offset = row % 2
col = (i % n) + offset
return row, col
def fillSquare(x,y):
pen = Turtle()
pen.hideturtle()
pen.speed(10)
pen.up()
pen.goto(x,y)
pen.fillcolor("black")
pen.begin_fill()
def main():
x = size()
y = draw(x)
row, col = findGrid(x) #I think there is a problem here. Please help!
f = fillSquare(row, col)
main()
答案 0 :(得分:-2)
将尺寸功能中的input
更改为raw_input
def size():
size = eval(raw_input("Please enter the size of the checkered flag: "))
return size
eval
处理字符串,并传递 int 值。