Need help in project


I need help on the RGB to Hex converter project. I'm trying to run it but it says there is something wrong with my code.

here is my code, can someone please check what is wrong with it because i can't figure it out:

def rgb_hex():
invalid_msg = "Sorry, that is invalid."
red = int(raw_input("Enter a red value."))
if red < 0 or red > 255:
print "Sorry, that is invalid."
green = int(raw_input("Enter a green value."))
if green < 0 or green > 255:
print "Sorry, that is invalid."
blue = int(raw_input("Enter a blue value."))
if blue < 0 or blue > 255:
print "Sorry, that is invalid."
print "%s" % hex(val)[2:].upper()

def hex_rgb():
hex_val = raw_input("Enter a hexadecimal value.")
if len(hex_val) != 6:
print "Sorry, that is invalid."
hex_val = int(hex_val, 16)
two_hex_digits = 2 ** 8
blue = hex_val % two_hex_digits
hex_val >> 8
green = hex_val % two_hex_digits
hex_val >> 8
red = hex_val % two_hex_digits
print "Red: %s Green: %s Blue: %s" % red, green, blue

def convert():
while True:
option = raw_input("Enter 1 to convert RGB to HEX. Enter 2 to convert HEX to RGB. Enter X to Exit: ")
if option == "1":
print "RGB to HEX..."
elif option == "2":
print "Hex to RGB..."
elif option == "X" or option == "x":
print "Error"


Someone please help me!


First off, is this a homework question? Or is it your own project?


It is the codecademy project at the end of unit 10.


Hi @maemoazzam ,

It is important to format code when you post it. That enables users to see your indentation and other important details.

See How do I format code in my posts?.

In this line, you are missing parentheses around the series of three variables, red, green, blue ...

print "Red: %s Green: %s Blue: %s" % red, green, blue


This is meant to be used in the print statements:

print invalid_msg

We can improve on this (slightly) by creating a function at the very top of our program:

def invalid_msg():
    print "Sorry, that is invalid."

Then, anywhere in the code you wish the message to print,


We can improve upon this code, as well:

if red < 0 or red > 255:
    print "Sorry, that is invalid."

may be written,

if red not in range(256):

We need to assign this back onto the same variable...

hex_val = hex_val >> 8


elif option.upper() == "X":

@appylpye already covered the error in your print statement.


Thanks, I'll add this onto the code and check if it works


It works but there is just 1 thing i cant figure out it keeps on saying:

Traceback (most recent call last):
File "", line 49, in
print convert()
File "", line 40, in convert
print rgb_hex()
File "", line 15, in rgb_hex
print "%s" % hex(val)[2:].upper()

can anyone tell me what it means.


Hi, @maemoazzam ,

You may not have defined val. See instruction 11 ...

  1. On the next line, outside of any if statement, create a variable called val. Set it equal to the sum of shifting red to left by 16 bits, shifting green to left by 8 bits, and blue.

Assuming that red, green, and blue have been set correctly, and still need to be shifted, you can do this ...

val = (red << 16) + (green << 8) + blue



Python 2.x.x won't let us do the following, but Python 3.x.x will,

invalid_msg = lambda: print "Sorry, that is invalid."

which is called the same as the function above...


@appylpye, am I on the wrong track with this idea?


To get it to work with Python 3.x, we need to pass the string to the print function as an argument, within the lambda. Just add parentheses ...

invalid_msg = lambda: print("Sorry, that is invalid.")

Then, you can call it when you need it ...


It is true that we cannot do this in Python 2.x, because print is not a function in that version of Python, and therefore does constitute an expression.


My bad missing the parens. Happens when not pasting in tested code. I should have clued into the expression aspect as why the lambda won't work in 2.x. Now that you bring it up I can see how this makes sense. Thanks.

Which approach would you take in this instance, the function or the lambda? Regardless that it works and is valid, it would help to understand any drawbacks or weaknesses.


When I did the project, I just used the approach that the instructions suggested ...

Inside the method, create a variable called invalid_msg and set it equal to an appropriate error message.

Then, when the message was needed, I just printed it, using the name of the variable.

However, if this were a larger project with many different options for input that the user could choose, and I wanted to standardize the style of the error messages, but customize the content of the message for each situation that might arise, I would probably write it as a named function with parameters. That, as opposed to a lambda, would make it easy to use multiple statements, if needed, in order to compose the output in several steps.


Sorry, I accidentally skipped the step where it tells us to define val. After I defined it it worked. Thanks for your help.