# Invalid Entry

#1

The code itself works, but for some reason prints "Invalid Entry" even after giving the conversions.

I believe it is something small, I just can't see it.

``````def rgb_hex():
invalid_msg = "Invalid Entry"
red = int(raw_input("Red Value: "))
if (red < 0 or red > 255):
print invalid_msg
return
green = int(raw_input("Green Value: "))
if (green < 0 or green > 255):
print invalid_msg
return
blue = int(raw_input("Blue Value: "))
if (blue < 0 or blue > 255):
print invalid_msg
return
val = (red << 16) + (green << 8) + blue
print "%s" % (hex(val)[2:]).upper()

def hex_rgb():
hex_val = raw_input("HEX Value: ")
if len(hex_val) != 6:
print "Invalid Entry"
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: %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: ").upper()
if option == "1":
print "RGB to HEX: "
rgb_hex()
if option == "2":
print "HEX to RGB: "
hex_rgb()
if option == "X":
break
else:
print "Invalid Entry"

convert().``````

#2

because if you enter anything other then 'x' the else-branch will be executed ---> and it prints "Invalid Entry"
you should elif-statements instead multiple if-statements.

``````if option == "X":              # if you enter x or X --> break
break
else:                          # if it wasn't x or X (including 1 or 2) ---> print "Invalid Entry"
print "Invalid Entry"``````

#3

Got it, thank you! Silly me and copy/paste.

#4

Also, I have noticed that the indentation here should be different, even though, by en large, I don't think it affects the overall functionality in any way that I can see:

``````else:
hex_val = int(hex_val, 16)
two_hex_digits = 2 ** 8
blue = hex_val % two_hex_digits
hex_val = hex_val &gt;&gt; 8
green = hex_val % two_hex_digits
hex_val = hex_val &gt;&gt; 8
red = hex_val % two_hex_digits
print "Red: %s Green: %s Blue: %s" % (red, green, blue)``````

it should actually be:

``````def hex_rgb():
hex_val = raw_input("Enter the color (six hexadecimal digits): ")
if len(hex_val) != 6 :
print "Invalid hexidecimal value. Try again."
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: %s Green: %s Blue: %s" % (red, green, blue)``````

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.