Article 4XYWF Why this simplest code is fine with python2 but is wrong with python3?

Why this simplest code is fine with python2 but is wrong with python3?

by
dedec0
from LinuxQuestions.org on (#4XYWF)
I do not know python.

I am doing a basic course that uses python, but it does not requires programming knowledge in it, or any other language - although they will help a lot. Basically, this means i skip a few things the course teaches. Anyway, it is giving me very basic examples of python code.

Right now, i am in this situation. First, i have a CSV data file:

Code:Cidade, Estado
Mariana, MG
Betim, MG
Salvador, BASecond, i have the source code:

Code:#!/usr/bin/python

# Assign a variable name to a data file.
varReadFile = open('/dev/shm/data.csv')

# For each line in the file, read it
for varLine in varReadFile:
if 'Betim' in varLine:
# Each time a line is read,
# print it to the screen
print ( varLine )The course is made for possible programming newbies, so they mostly do things to work in python 3.6 in a Win10 enrironment, but giving quick directions to the other options (like mine, in Debian). But i have even installed the IDLE Python thing, just to try it, since the course mentioned its friendlyness. First, i really hated its white bg theme... but that was fixable with something i searched around the web (just in its settings was not enough in easy or quick steps). But it still more awkward then my usual workplace of a terminal with a few tabs, source on one, compiler called in command line in other, and sometimes another tab with something else.

Anyway, i ended up finding that my usual "programming habits" are also possible with python, although the course did not mention it.

I did not have to install python 2 or 3 in my Debian 9. Both were already installed. 2.7 and 3.5 - and i did not feel a need to update that, since i have done it in very few weeks (maybe days) ago. I installed just the IDLE Python (for both python versions).

So, using the terminal with vim to edit the source (with my usual settings for indentation and everything else), and compiling with simple commands, i have this output, after saving the two files above in the same dir:

Code:$ python2 code.py
Betim, MG

$ python3 code.py
File "ex.03.py", line 11
print ( varLine )
^
TabError: inconsistent use of tabs and spaces in indentation

$ python3 code.py # After changing spaces in line 11 (...)
File "ex.03.py", line 11
print ( varLine )
^
TabError: inconsistent use of tabs and spaces in indentation

$I tried changing the spaces in lines 11 in a good number of ways, and none worked.

Please notice that the indentation i use with Vim is 4 spaces sized, but tabs are used to transform 8 consec. spaces into 1 tab.

Directly, the vim settings i edited *and copied* the source code given above, are:

Code:set noexpandtab # this line is not even in my rc, and not anywhere else
set tabstop=8 # this line is not even in my rc, and not anywhere else
set softtabstop=4
set shiftwidth=4all in my ~/.vimrc

What is wrong with me (probably) or python3?latest?d=yIl2AUoC8zA latest?i=AwFQdzk31Jk:RjpILlQQTZs:F7zBnMy latest?i=AwFQdzk31Jk:RjpILlQQTZs:V_sGLiP latest?d=qj6IDK7rITs latest?i=AwFQdzk31Jk:RjpILlQQTZs:gIN9vFwAwFQdzk31Jk
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments