1

I have started coding for about a week, and while practicing to create a shape calculator, I encountered such an error:

Traceback (most recent call last):
File "python", line 4
if option = 'C':
          ^
SyntaxError: invalid syntax

The code is as follows:

print "The Calculator has been launched"
option = raw_input ("What shape is your object?     Enter C for circle or T 
for Triangle.")
if option = 'C': 
    radius = float (raw_input ("What is the radius of your circle?") )
    area_1 = 3.14159 * ( radius ** 2)
    print area_1 

elif option = 'T':
    base = float (raw_input ("What is the base of the triangle?"))
    height = float (raw_input ("What is the corresponding height of the 
    triangle?"))
    area_2 = (base * height) * 1/2
    print area 
else :
    print "Please, enter a valid shape" 

I would be very grateful if someone could explain the cause of the error.

Thanks!

Lazimsiz
  • 23
  • 1
  • 1
  • 3

4 Answers4

4

When comparing you must use a ==. The = is only used for assignment.

So in your example the line should be

if option == 'C':
ltd9938
  • 1,444
  • 1
  • 15
  • 29
  • Thanks for the feedback! == seems to have fixed the issue partially, but now I have this error. Any ideas on how it can be fixed? Traceback (most recent call last): File "python", line 6 area_1 = 3.14159 * radius**2 ^ IndentationError: unindent does not match any outer indentation level – Lazimsiz Jun 29 '18 at 16:00
  • That means the indentation isn't lining up. Double check the indentation for that line. https://stackoverflow.com/questions/14979224/indentation-error-in-python – ltd9938 Jun 29 '18 at 16:02
2

Yes it is actually a very basic error that everyone does in the beginning :)
The = operator does not mean the same thing in code as it does in math. Here it means that you want to assign a value to a variable (you can also think of it as the := operator that you can see in maths or other coding languages).

The operator you need to compare two elements is == which returns a boolean value: either True or False

Zuma
  • 806
  • 1
  • 7
  • 10
1

It is worth to mention that this code will be difficult for user to work beacue of nescessity of inputting big letters (Shitf + letter). To avoid that, just use lower() method.

if option.lower() == "c":
   do_something()

Now, user can input both big or small letter ("c" or "C"), and program will be no differ to that. Of course the nesscesity of using "==" in any comparing is necessity.

0

you can also use 'is'

if option is 'C':

Don't use the equality "==" symbol to compare objects to None Use "is" instead

"etc" is None  # => False
None is None  # => True
# negate with not
not True  # => False
not False  # => True

# Equality is ==
1 == 1  # => True
2 == 1  # => False

# Inequality is !=
1 != 1  # => False
2 != 1  # => True