Why won't this work?

Hello.
I am doing the exercise on rgb to hex converter, and I have completed all the code, and, afterwards, looked at the hints. Yet when I run, it provides this error:

'On line seventeen
def hex_rgb():
  ^
SyntaxError: invalid syntax

Here is the actual code (I have removed the interior of the functions to condense it.

def rgb_hex():
def hex_rgb():
def convert():
convert() 

Thanks!

now we might not be able to find the issue. Don’t condense the code

consider this:

print("hello world"
x = 3

now i get a syntax error on line 2, while the error is on line 1 (missing closing parentheses)

same is very likely true for your error. But i can’t see that now due to the condensed code.

3 Likes

Not really condensed, is it. More like completely missing… :slight_smile:

1 Like

Here is the full code(in python 2):

def rgb_hex():
  invalid_msg = 'Incorrect values entered.'
  red = int(raw_input('Enter red (R) value:')) 
  if (red < 0 or red > 255):
    print 'Invalid red value.'
    return
  green = int(raw_input('Enter green (G) value:')) 
  if (green < 0 or green > 225):
    print 'Invalid green value.'
    return
  blue = int(raw_input('Enter bblue (B) value:')) 
  if (blue <0 or blue > 225):
    print 'Invalid blue value.'
    return
  val = (red << 16) + (green << 8) + blue
  print '%s' % (hex(val)[2:].upper() 
def hex_rgb():
  hex_val = raw_input('Enter hexadecimal value: ')
  if len(hex_val) != 6:
    print 'Invalid hexadecimal value entered.'
    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: ')
   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() 

Is this statement complete?:

  print '%s' % (hex(val)[2:].upper() 

It is the final line in the rgb_hex function definition.

1 Like

No, it should have a closing parentheses for the hex function! Thank you very much!

1 Like

I guessed the error correctly:

when dealing with syntax error, always check the line above as well.

4 Likes

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