I have tried the python coding challenge of the rgb to hex converter and here is my code.

```
def rgb_hex():
invalid_msg = "Invalid value entered"
red = int(input("Enter a red value: "))
if red < 0 or red > 255:
print(invalid_msg)
return
green = int(input("Enter a green value: "))
if green < 0 or green > 255:
print(invalid_msg)
return
blue = int(input("Enter a blue value: "))
if blue < 0 or blue > 255:
print(invalid_msg)
return
val = (red << 16) + (green << 8) + blue
print(str(hex(val[2::]))).upper()
def hex_rgb():
hex_val = input("Enter the hexadecimal value: ")
if len(hex_val) != 6:
print("Invalid hexadecimal value")
return
else:
hex_val = int(hex_val, 16)
two_hex_digits = 2 ** 8
blue = hex_val % two_hex_digits
hex_val = hex_val >> 8
green = hex_val % two_hex_digits
hex_val = hex_val >> 8
red = hex_val % two_hex_digits
print("Red:" , red , "Green:" , green , "Blue:" , blue)
def convert():
while True:
option = 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..."
rgb_hex()
elif option == '2':
print("Hex to RGB...")
hex_rgb()
elif option == 'X' or option == 'x':
break
else:
print("Error")
convert()
```

However, whenever I enter 1 or 2, the code just runs the print(âErrorâ) message, and gets me to enter a message again. And when I enter x it tells me that the name âxâ is not defined. Someone please help!