How to draw a checkered flag to the Python screen?

2024/11/7 10:44:35

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) + offset

Please 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 sizedef 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 // noffset = row % 2col = (i % n) + offsetreturn rowreturn coldef 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()
Answer

If you want to return two values, you must combine them in some way. If you do this:

return row
return col

the 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, col

The 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.

https://en.xdnf.cn/q/120752.html

Related Q&A

Open txt file in python

i need to open a txt file . In txt file i have Andrei:Popescu:Bucuresti Maria:Popescu:Targu-Mures ....How do I read a text file into three variable and for each line do something ? Sorry for my englis…

Python 3.3.3 time.sleep() error [duplicate]

This question already has answers here:Python: "global name time is not defined"(8 answers)Closed 10 years ago.Im getting the following error:The error:"Traceback (most recent call last)…

what is the decimal.getcontext().copy() mean [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

how do I change a python interpreter from english to my dialect

I have been stuck for months trying to edit the python interpreter (from www.python.org). All I want to do is to change the keywords eg. change from. English language: print() to Ibo language de().

Python, How to make a Video File(Mpeg/avi etc) into an exe?

First of all, I really dont know the correct direction on where to search or what to search.Going to the point of problems.How can I make my Video File into an exe format?I really need your help guys,…

Is it possible to avoid integers,floats and special characters using try-except statement only?

Im trying to make a code that allows only letters. I know we can do this using isalpha() method. But, Im looking for any other different solutions something like try-except?

Similarity between two text documents in Python

You are provided with four documents, numbered 1 to 4, each with a single sentence of text. Determine the identifier of the document which is the most similar to the first document, as computed accord…

Aligning strings in Python

I searched for creating aligned strings in Python and found some relevant stuff, but didnt work for me. Heres one example:for line in [[1, 128, 1298039], [123388, 0, 2]]:print({:>8} {:>8} {:>8…

Program fails in Python 2.7.8 but runs in Python 3.4.1

Ive ran this code using Python 3.4.1 and it works, but if I use Python 2.7.8 it fails, why?i=1 while i<10:for x in(1,2,3,4,5,6,7,8,9):print (i*x,\t,end=)if x==9: print(\n)i=i+1

Clustered Stacked Bar in Python Pandas [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…