Python Forum
syntax error: can't assign to operator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
syntax error: can't assign to operator
#1
Below is my code, the error was "syntax error: can't assign to operator" at line 6, " L**2 - (2*a**2*L) + (16*area**2)= 0 ". I need help in debugging this error, thanks!



def inverse_acute_heron(a, area):
    a > 0
    area > 0
    b <= a + a
    L = b**2
    L**2 - (2*a**2*L) + (16*area**2)= 0
    d = (2*a**2)**2 - 4*(16*area**2)
# a=1 b=2*a**2 c=16*area**2
    if d == 0: #only gives 1 solution
        x = (-(2*a**2) + sqrt(d)) / 2
    elif d > 0: #gives 2 solution
        x1 = (-(2*a**2) + sqrt(d)) / 2
        x2 = (-(2*a**2) - sqrt(d)) / 2
    return x1 and x2
print(inverse_acute_heron(1, 0.5))
print(inverse_acute_heron(1, heron(1, 1, 1)))
Reply
#2
what is following line supposed to do
L**2 - (2*a**2*L) + (16*area**2)= 0
it's not a valid syntax
Also note that when you resolve the above issue
a > 0
    area > 0
    b <= a + a
will have no visible effect whatsoever, i.e. it's a valid code/syntax, but it does nothing that affects the rest of the code
then the return statement
 return x1 and x2
will not give you what you [probably] expect e.g.
>>> 1 and 2
2
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Sorry, to clarify, the full question is as follows:

Heron’s formula is a method for calculating the area of triangle given the lengths of all three sides. We already know that Heron’s formula is given as:
area = sqrt(p(p-a)(p-b)(p-c))

where p is half the perimeter: p = (a+b+c)/2

Your task is to write the function inverse_acute_heron(a, area). The function accepts two numbers 'a' and 'area' corresponding to the length a and the area of triangle respectively. The function returns the length of the missing side b corresponding to an acute angle triangle.
Assumptions:
a > 0
a + a >= b
area > 0

you should rewrite the Heron’s formula into some other forms. It will be beneficial to simply let L=b^2. This will allow you to solve some form of quadratic equation, which you can simply use a modified version of solve_qe to solve.
A good starting point is:
area^2 = ((2a+b)/2) * (((2a+b)/2) - a) * (((2a+b)/2 - a)) * (((2a+b)/2 - b))
Reply
#4
OK, some basics in programming -
= means assignment, not equality. a=b is not an assertion, rather it is a command to take the value of b and assign that to variable a.
a > 0 is meaningless. It does not tell the python interpreter to do anything - you are not giving it an assumption

So, lines 2, 3, and 4 don't actually do anything of use.
Line 5 takes the value of b, squares it, and stores that in L. However, you have not given b a value so this will give you an unassigned value error.
Line 6 would be interpreted as assigning the value of zero to some variable, but the left side of the assignment is not a variable. Therefore it cannot work.
Line 7 is your first valid syntax.

Suggest - you are given a and area. Take your starting point formula and use a little algebra to solve for b. Then program that formula in python.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked School project -- need help with it it says syntax error XPGKNIGHT 6 3,308 Aug-11-2022, 08:43 PM
Last Post: deanhystad
  I'm getting a syntax error very early on in my code and I can't quite figure it out. liloliveoil 1 1,997 Oct-30-2020, 05:03 AM
Last Post: deanhystad
  Unspecified syntax error hhydration 1 2,001 Oct-25-2020, 10:45 AM
Last Post: ibreeden
  Annuity function for school - syntax error peterp 2 1,966 Oct-12-2020, 10:34 PM
Last Post: jefsummers
  Invalid syntax error, where? tucktuck9 2 3,417 May-03-2020, 09:40 AM
Last Post: pyzyx3qwerty
  Raise an exception for syntax error sbabu 8 3,140 Feb-10-2020, 01:57 AM
Last Post: sbabu
  Self taught , (creating a quiz) syntax error on my array DarkAlchemyXEX 9 4,237 Jan-10-2020, 02:30 AM
Last Post: ichabod801
  If Statements and Comparisons returns a syntax error DarkAlchemyXEX 2 2,432 Jan-02-2020, 01:25 PM
Last Post: DarkAlchemyXEX
  Syntax Error: Invalid Syntax in a while loop sydney 1 4,068 Oct-19-2019, 01:40 AM
Last Post: jefsummers
  syntax error in an if statement at the else ? Just_started 2 2,231 Mar-07-2019, 08:35 PM
Last Post: Just_started

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020