[Tutor] writing function changeColor

Prasad, Ramit ramit.prasad at jpmorgan.com
Wed Jul 18 22:04:43 CEST 2012


Please do not top post.

> I am trying my best to learn and your attitude was not welcome. If you don't
> want to answer my question, I understand. I'm doing research elsewhere too and
> because I am new at this, my familiarity with key terms and search words is
> also improving, yet at a slower pace. My goal is to find people who would help
> me find the resources I need and not cheat, which is why I didn't want to post
> my assignment.

> > > I am trying to write a function changeColor for an assignment. Two things that I am unsure about for this assignment are how to assign different colors to integers so that, red will be 1, blue will be 2, etc. Also, I learned python so that if I were to put in 0.9, it'd decrease red by 10%. The way this function needs to be written, -0.1 decreases red by 10%

The problem is that you have given us an incomplete description of what
you need. Without a full description, it is difficult to answer without
making a host of assumptions that are probably all invalid. If you can 
provide a full description then we can help better. Even a code sample 
would give us some context. 

From your original email, I have no idea what UI framework you refer to
(if any), how you get a color or change a color or anything meaningful. 
Although, you might not like Bob's tone he makes a very valid point. 
If you learn to ask better questions, you get better answers.

Thankfully you ended up replying to Emile with some code so I can help
better.

> I was told to make different names for the float and integer (float = scale
> and color= integer) and then I kept everything else the same, just to test it
> out and see if that would work.
> 
> So it looks like this:
> 
> def changeColor(pict,scale,color):
>   for p in getPixels(pict):
>     value=getRed(p)
>     setRed(p,value*.9)
> 
> Unfortunately I keep getting this error message:
> 
> The error was:changeColor() takes at least 3 arguments (1 given)
> Inappropriate argument type.
> An attempt was made to call a function with a parameter of an invalid type.
> This means that you did something such as trying to pass a string to a method
> that is expecting an integer.

Please always post the exact trace back and not just a portion.
It might not seem like all that is needed is the error message
but it can make a big difference in the amount of help.

Luckily, I can tell what the problem is from this error. You are using 
`changeColor(picture)` which no longer works since you changed changeColor 
to take 3 arguments so you need to change each call to 
`changeColor(picture,scale,color)`.

Also, instead of saying getRed(p), I would do getColor(p, color).
Something like below; although, I leave the implementation of
getColor/setColor up to you. Oh, and I fixed your indentation problem.
(Could have been caused by posting in rich/HTML text and not plain text)

def changeColor( pict, scale color ):
''' Change the intensity of color.
scale can be -1.0 <= x <= 1.0
'''
    for p in getPixels(pict):
        value=getColor(p,color)
        change = 1.0 + scale # -.1 now equals .9 
        setColor(p, color, value * change )


Often in Python, you do not worry about whether a number
is a float or an int (unless it happens to be part of the assignment), 
you just do what you need and check that it worked.
You can wrap scale and color in float() or int() respectively
if you want to explicitly convert them.

It might make more sense to take in a % instead, so -10 for scale lessens the 
color by 10%. Lots of UI does things by this using 0.0-1.0 values (as given
in your example), so it makes sense, but it might be easier to think about 
as a percentage. 

Ramit

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list