问题描述
$ b
如果您想要返回以某种方式结合他们。如果你这样做的话:
$ $ $ $ $ $ $ c $ return $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ pre>
程序将返回 行然后退出函数,因为这就是 return 所做的。在执行第一个 return 之后什么都不会执行。试试这个:
return row,col
返回的值是一个元组,这正是您需要执行的操作 row,col = findGrid(x)出现在你的 main()中。而不是计算单个 int , findGrid(x)将取代一个元组包含两个 int s,Python可以迭代元组来将每个值放入指定的变量行和 col 。
由Python解释器生成的消息通常是相当丰富的。在这种情况下,当它说 int对象不可迭代时,可以打赌它试图迭代 int 可以理解的是失败了。所有你需要做的就是推断出错误的语句是在哪里寻找一个可迭代的,找到产生有问题的表达式( findGrid(x)),并检查它是否是返回一个 int 或者一个可迭代的。
QUESTION: Implement the following pseudocode to draw a checkered flag to the screen.
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) + offsetPlease copy and paste the link see the ouput: http://www.awesomescreenshot.com/image/45977/12eaef67de44c2b291ecd47fe8d10135
I implemented the pseudocode, but I need some help. I am keep getting this error: row, col = findGrid(x)TypeError: 'int' object is not iterable
My program:
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 return 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) f = fillSquare(row, col) main()解决方案If you want to return two values, you must combine them in some way. If you do this:
return row return colthe program will return the row and then exit the function, because that's what return does. Nothing after the first return will ever be executed. Try this instead:
return row, colThe returned value will be a tuple, which is exactly what you need to carry out row, col = findGrid(x) as appears in your main(). Instead of evaluating to a single int, findGrid(x) will instead evaluate to a tuple containing two ints, and Python can iterate over that tuple to place each value into the specified variables row and col.
The error messages generated by the Python interpreter are usually pretty informative. In this case, when it says int object is not iterable, you can bet that it tried to iterate over an int and understandably failed. All you have to do then is deduce where the erroneous statement in question is looking for an iterable, find what produces the problematic expression (findGrid(x)), and inspect whether it returns an int or an iterable.
这篇关于如何在Python屏幕上绘制方格旗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!