Elif option == 'T': syntax error. Tried many things. Can't figure it out


I keep getting an unspecific syntax error on
elif option == 'T':
I've looked at other forum posts, tried various indentations and even copy/pasted stuff into my code to see if it would work but it doesn't appear to. Can anyone see what's wrong with my code? Any help is appreciated.

from math import pi
# imports a thing
from time import sleep
# imports date and time info
from datetime import datetime

now = datetime.now()

print ("Welcome to the severly limited Calculator!")
print ("Current time: %s %s %s %s %s") % (now.month, now.day, now.year, now.hour, now.minute)


hint = "Don't forget to include the correct units, bruh. \nExiting..."
option = raw_input("Welcome, will you be calculating a circle or a triangle today? C/T: ")
option = option.upper()

if option == 'C':
   radius = float(raw_input("Wut ur radius, bruh?: "))
   area = pi * radius**2 
   print 'Pi is baking. Get it?'
print ("Area: %.2f. \n%s" % (area, hint))
elif option == 'T':
  base = float(raw_input("What is the base of the triangle?: "))
  height = float(raw_input("Triangles height: "))
  area = (0.5) * base * height               
  print ("hmm...")   
print ("Area: %.2f. \n%s" % (area, hint))
 	 print ("I sed enter 'C' or 'T', bruh")


The print above the elif is outside of the if block, so the elif is unexpected.


Well, I had print indented before, but I kept getting an 'unexpected indent' error from that line. So I removed the indent from print. While that got rid of the 'unexpected indent' error, I now get an error one line down on the elif statement.


An unexpected indent can be triggered by unevenness which is tabs and spaces mixed. Never a good thing. The manual fix is to do all the indentation over, 4 spaces for each level.

def foo():
    return "bar"

This is something I have done a thousand times. Manually respacing the indents. Frustrating and time consuming as it is. Most times, if we write code and don't edit it too much, we can trust the editor indents.

After that it's just knowing when to unindent.


It works! Excellent! Thanks so much. Is there a way to set tab to equal 4 spaces? That'd clear up the spacing problem, right? And spacing indicators/lines so that you know where to space things too.


The spacing problem is something we introduce. If all the code we enter is done by the editor, and it is correct, then there will be no problems. If we start editing the code we stand a chance of creating imbalance (mixed s/t).

There is no automatic soluton. But there is no set rule on how large an indent can be.

>>> def foo(bar):
  if bar.isalpha():
    while len(bar) > 0:
      bar = bar[1:]
    bar = bar[::-1]
  return bar

>>> foo('brace yourself')
'flesruoy ecarb'
>>> foo('braceyourself')

The only real importance is that the indentations match and make sense to Python (meaing it is critical). When an editor messes up, go back to square one and manually set all indents, according to block scope.


So could I actually just use tab instead of hitting space four times and get working code? Or is there a specific reason that four spaces is best?


Tabs are not always set the same from one machine to another. A space is always a space, no matter what. Form the habit of using spaces and it will become part of your wirting. There is no hard and fast rule, only the need for consistency, accuracy with indents, and readability. The little extra effort will go a long way.


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