0

I have this code:

def Gradiente(grado):
   suma = 0
   for i in range(porcent):
       x1 = entrenamiento[i][0]
       x2 = entrenamiento[i][1]
       y = entrenamiento[i][2]
       h = 1 / (1 + math.exp(-(t0 + t1 * x1 + x2))

       if (grado == 1):
           suma = suma + (h - y)
       elif(grado == 2):
           suma = suma + (h - y) * x1 

   return suma / porcent

at the if, it keeps saying invalid syntax (at the ":"), is it wrong?

7
  • 3
    in python indentation matters Commented Nov 13, 2013 at 6:23
  • 8
    Missing closing ) for this expression: (1 + math.exp(-(t0 + t1 * x1 + x2)) Commented Nov 13, 2013 at 6:24
  • Indent all the code below def Gradiente(grado): Commented Nov 13, 2013 at 6:24
  • i know about identation is just when i paste the code, the correct answer is the missin ")" thanks all Commented Nov 13, 2013 at 6:31
  • @hcwhsa put the answer so i can give you the credits. Commented Nov 13, 2013 at 6:34

2 Answers 2

3

97.2% of all problems people have with Python involves incorrect indentation :-) In your original question, your indentation was incorrect and you need to indent it properly:

def Gradiente(grado):
    suma = 0
    for i in range(porcent):
        x1 = entrenamiento[i][0]
        x2 = entrenamiento[i][1]
        y = entrenamiento[i][2]
        h = 1 / (1 + math.exp(-(t0 + t1 * x1 + x2)))  # <-- fix

        if grado == 1:                                # <-- style
            suma = suma + (h - y)
        elif grado == 2:                              # <-- style
            suma = suma + (h - y) * x1 

    return suma / porcent

However, you've since made it clear that was a typo on your part when entering the question in which case it'll simply be the missing close-parenthesis on that large mathematical formula (which is also fixed in my code above).

One other change made is the removal of superfluous punctuation from the if statements. People who use them tend to come from a C (or similar language) background where they're necessary. Using them in Python usually just clutters the code unnecessarily.

Sign up to request clarification or add additional context in comments.

Comments

1

Indent everything one tab after line 1. Indentation matters in Python.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.