Syntax Error for Function Definition


#1

In run testing the RGB to Hex converter, following the complete exercise, I am receiving a syntax error message that looks like this:

$ python rgb2hex.py
   File "rgb2hex.py", line 22                                 
    def hex_rgb():
        ^                                                  
SyntaxError: invalid syntax

Naturally I'm not expecting this, as I've ensured that the spacing is even (though I did use the tab key in favour of the spaces.)

Any ideas as to what's happening? Here's the code below:

def hex_rgb():
    hex_val = raw_value("Enter a hexadecimal colour value: ")
    if len(hex_val) != 6:
        print "Invalid 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 "R:s% G:s% B:s%" % (red, green, blue)


#2

should be raw_input().


#3

@collective_singular ,

You haven't posted all your code.

According to this portion of the error message ...

   File "rgb2hex.py", line 22                                 
    def hex_rgb():

... the following function header is the 22nd line of your code ...

def hex_rgb():

The SyntaxError error is evidence of a problem prior to that line, possibly in the line that immediately precedes it.


#4

@mtf, thanks for picking that one out. Odd mistake to make, as it was in plain sight.

Here's the edited version of the code. It's still returning the same error. @appylpye I've included the prior block for proper formatting and troubleshooting.

Thanks for your help on this.

def rgb_hex():
  invalid_msg = "Invalid value."
  red = int(input("Enter the 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
  value = (red << 16) + (green << 8) + blue
  print "s%" % (hex(value[2:]).upper()

def hex_rgb():
    hex_val = raw_input("Enter a hexadecimal colour value: ")
    if len(hex_val) != 6:
        print "Invalid 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 "R:s% G:s% B:s%" % (red, green, blue)

#5

Hi @collective_singular ,

This is the statement, on line 20, that precedes the function header that is on line 22 ...

print "s%" % (hex(value[2:]).upper()

Check the parentheses.

When you get a SyntaxError, it is often a good idea to check what precedes the line on which the error was reported.


#6

There it is! (Argh!)

Thanks for picking that out.


#7

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