Thread Shed | Using return value of a function in a seperate codeblock

Hello I am not going to link the project because while my question stems from the project, it is not entirely related. So in the assignment we are asked to create a function and then a list of colors.

Which I have done so:

def color_count(color):
  cc = 0
  for x in colorscleard:
    for y in x:
      #print(y)
      if y != color:
        #print(y)
        continue
      elif y == color:
        cc += 1
      else:
        return"Something went wrong!"
  return cc
        
colorlist = ['red','yellow','green','white','black','blue','purple']
 

z = 0  
while z < len(colorlist):
  color_count(colorlist[z])
  print("Today " + cc + " amount of " + colorlist[z] + " were sold.")
  z += 1

So then I wanted to create a while loop to run the entirety of the color list and see for myself if the while code would work. While my initial function works, the while code doesn’t because I am unable to acquire the value for cc whenever it runs the function above.

What do I need to do if I want the code to automatically use the return value of a function at a completely different code block? Is there a special phrase for it?

Welp one way I found out how to fix this was via:

z=0
while z < len(colorlist):
  omer=color_count(colorlist[z])
  print("Today " + str(omer) + " amount of " + colorlist[z] + " were sold.")
  z += 1

Some problems are best solved with the tool that is meant for the job, especially with it being so close at hand… for.

for color in colorlist:
    print (f"Today {color_count(color)} amount of {color} were sold.")
1 Like