Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+6 votes

Right now I have a function that returns objects in a for loop from a different line, but it's only returning the first expression through the loop.

   def display(artFileName):
        while data != '':
            data = getPixels(artFileName)
            print (data)

    def getPixels(artFileName):
        for line in fileLine[1:]:
            return (pointX, pointY, red, green, blue) 

Right now it's only printing the first line that gets run through the loop. How do I get the def display() to print all of the lines that get run through getPixels?

(When I print the loop in getPixels it displays the full list.)

asked in CSC201 Spring 2021 by (1 point)

1 Answer

+2 votes

We know that return can only get 1 value and then end the loop and function immediately. But you can print and write line to output file multiple times in the for loop.

If you want to get the data variable return from the function getPixels with full list of pixels, in def getPixels, you can create a temporary list, or dictionary, append the pixel to it in each loop, and then return the list after the for loop at the end of the function.

answered by (1 point)