Indentation error


#1

Can someone explain to me why this returns an indentation error.

	cell=(f_temp-32)*5/9
  return cell```

#2

It should be obvious. The two lines of code should line up. Are you mixing tabs and spaces?


#3

Im actually having a hard time with the code function on this forum, ill just copy paste plain text.

def f_to_c(f_temp):
     cell=(f_temp-32)*5/9
     return cell

f100_in_celcius=f_to_c(100)
print(f100_in_celcius)

man those 2 lines under f_to_c(f_temp) are indented before I post I swear lol.


#4

Looks like the indents are spaces. Should be working as expected.

Aside

Fahrenheit

Celsius

For simple functions we can return the expression directly…

def f_to_c(f_temp):
    return 5 * (f_temp - 32) / 9

#5

I see, I was using two spaces instead of tabbing, I figured it didn’t make a difference but im slowly coming to realize the little things count. Thank you for your help.


#6

Spaces are best, but we do need to be careful of auto-indent. It may use tabs.


#7

Btw, what is the difference between tab and space?


#8

A tab is preset in the editor but is a single block of spaces. Size of tabs may vary. Spaces are always 1 space per hit of the spacebar.


#9

Another way to think of their difference is that SPACE is a printable character, ordinal 32. Tab is an escape character (\t) and is technically in the ordinal range where non-printing characters are generally mapped (below 32). Tab is ordinal 11, if I’m not mistaken. The backspace is ordinal 8. I don’t think we can mess with this.

Tabs are actually stops just like on typewriters where we can set tab-stops. Out of the box, one tab could be a ten character interval, in some cases, or they could be intervals of 1/20th of the window width. Clearly, we need to intervene (or have our applications intervene) so we get a feel for how tabs behave.

Line printers have their own built in definition. Escape characters are generally seen by printers as instruction sequences which the printer executes. The ordinals mapped below 32 are, one suspects, universal and immutable, and likely mapped into the hardware of the printer. Mind, my knowledge base is very old and not up to date in this matter.

Spread sheets regard tabs as columns. If you have some tab separated data and paste it into A1 of a new worksheet, it will assume as many columns as needed. That’s where the name TAB comes from. It facilitates tabular data in a terminal, text editor or spread sheet.

>>> print ("{}\t{}\t{}\t{}".format('one', 'two', 'three', 'four'))
one	two	three	four
>>> 

Shell output copied and pasted into Excel…
tabular_data

Carriage-Return with Line-Feed is ordinal 13.

>>> print ("{}{}{}{}{}{}{}".format('one', chr(13), 'two', chr(13), 'three', chr(13), 'four'))
one
two
three
four
>>> 

but we normally use the newline escape character, \n,

>>> print ("{}\n{}\n{}\n{}".format('one', 'two', 'three', 'four'))
one
two
three
four
>>> 

#10

Wow, that’s very detailed answer, thank you very much! :slight_smile: