Reputation: 301
I'm trying to copy paste the below python code to my macvim and i'm getting "Unexpected indent".
## Can build up a dict by starting with the the empty dict {}
## and storing key/value pairs into the dict like this:
## dict[key] = value-for-that-key
dict = {}
dict['a'] = 'alpha'
dict['g'] = 'gamma'
dict['o'] = 'omega'
print dict ## {'a': 'alpha', 'o': 'omega', 'g': 'gamma'}
print dict['a'] ## Simple lookup, returns 'alpha'
dict['a'] = 6 ## Put new key/value into dict
'a' in dict ## True
## print dict['z'] ## Throws KeyError
if 'z' in dict: print dict['z'] ## Avoid KeyError
print dict.get('z') ## None (instead of KeyError)
I checked if it was because of mixing tabs and spaces, but found nothing;
python -tt new.py
File "new.py", line 4
dict = {}
^
IndentationError: unexpected indent
Also found that it could be because of Automatic indenting by my Vim, and tried to disable it by using
:set paste
That did not work either. Below is my .vimrc file
set nobackup
set nowritebackup
set noswapfile
set lines=40
set columns=80
set tabstop=4
set shiftwidth=4
set softtabstop=4
set autoindent
set smarttab
set retab
set paste
filetype indent on
filetype on
filetype plugin on
This is the hexdump of the file
00000000 20 20 23 23 20 43 61 6e 20 62 75 69 6c 64 20 75 | ## Can build u|
00000010 70 20 61 20 64 69 63 74 20 62 79 20 73 74 61 72 |p a dict by star|
00000020 74 69 6e 67 20 77 69 74 68 20 74 68 65 20 74 68 |ting with the th|
00000030 65 20 65 6d 70 74 79 20 64 69 63 74 20 7b 7d 0a |e empty dict {}.|
00000040 20 20 23 23 20 61 6e 64 20 73 74 6f 72 69 6e 67 | ## and storing|
00000050 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 | key/value pairs|
00000060 20 69 6e 74 6f 20 74 68 65 20 64 69 63 74 20 6c | into the dict l|
00000070 69 6b 65 20 74 68 69 73 3a 0a 20 20 23 23 20 64 |ike this:. ## d|
00000080 69 63 74 5b 6b 65 79 5d 20 3d 20 76 61 6c 75 65 |ict[key] = value|
00000090 2d 66 6f 72 2d 74 68 61 74 2d 6b 65 79 0a 20 20 |-for-that-key. |
000000a0 64 69 63 74 20 3d 20 7b 7d 0a 20 20 64 69 63 74 |dict = {}. dict|
000000b0 5b 27 61 27 5d 20 3d 20 27 61 6c 70 68 61 27 0a |['a'] = 'alpha'.|
000000c0 20 20 64 69 63 74 5b 27 67 27 5d 20 3d 20 27 67 | dict['g'] = 'g|
000000d0 61 6d 6d 61 27 0a 20 20 64 69 63 74 5b 27 6f 27 |amma'. dict['o'|
000000e0 5d 20 3d 20 27 6f 6d 65 67 61 27 0a 0a 20 20 70 |] = 'omega'.. p|
000000f0 72 69 6e 74 20 64 69 63 74 20 20 23 23 20 7b 27 |rint dict ## {'|
00000100 61 27 3a 20 27 61 6c 70 68 61 27 2c 20 27 6f 27 |a': 'alpha', 'o'|
00000110 3a 20 27 6f 6d 65 67 61 27 2c 20 27 67 27 3a 20 |: 'omega', 'g': |
00000120 27 67 61 6d 6d 61 27 7d 0a 0a 20 20 70 72 69 6e |'gamma'}.. prin|
00000130 74 20 64 69 63 74 5b 27 61 27 5d 20 20 20 20 20 |t dict['a'] |
00000140 23 23 20 53 69 6d 70 6c 65 20 6c 6f 6f 6b 75 70 |## Simple lookup|
00000150 2c 20 72 65 74 75 72 6e 73 20 27 61 6c 70 68 61 |, returns 'alpha|
00000160 27 0a 20 20 64 69 63 74 5b 27 61 27 5d 20 3d 20 |'. dict['a'] = |
00000170 36 20 20 20 20 20 20 20 23 23 20 50 75 74 20 6e |6 ## Put n|
00000180 65 77 20 6b 65 79 2f 76 61 6c 75 65 20 69 6e 74 |ew key/value int|
00000190 6f 20 64 69 63 74 0a 20 20 27 61 27 20 69 6e 20 |o dict. 'a' in |
000001a0 64 69 63 74 20 20 20 20 20 20 20 20 20 23 23 20 |dict ## |
000001b0 54 72 75 65 0a 20 20 23 23 20 70 72 69 6e 74 20 |True. ## print |
000001c0 64 69 63 74 5b 27 7a 27 5d 20 20 20 20 20 20 20 |dict['z'] |
000001d0 20 20 20 20 20 20 20 20 20 20 20 23 23 20 54 68 | ## Th|
000001e0 72 6f 77 73 20 4b 65 79 45 72 72 6f 72 0a 20 20 |rows KeyError. |
000001f0 69 66 20 27 7a 27 20 69 6e 20 64 69 63 74 3a 20 |if 'z' in dict: |
00000200 70 72 69 6e 74 20 64 69 63 74 5b 27 7a 27 5d 20 |print dict['z'] |
00000210 20 20 20 20 23 23 20 41 76 6f 69 64 20 4b 65 79 | ## Avoid Key|
00000220 45 72 72 6f 72 0a 20 20 70 72 69 6e 74 20 64 69 |Error. print di|
00000230 63 74 2e 67 65 74 28 27 7a 27 29 20 20 23 23 20 |ct.get('z') ## |
00000240 4e 6f 6e 65 20 28 69 6e 73 74 65 61 64 20 6f 66 |None (instead of|
00000250 20 4b 65 79 45 72 72 6f 72 29 0a | KeyError).|
0000025b
Am i being silly and missing something very simple? Would you reccomend i move onto a Python based IDE like PyCharm?
Upvotes: -1
Views: 301
Reputation: 7946
In your hexdump there are two spaces before the first line of code. This means your initial indent was two spaces, and python expected no spaces (or tabs) at all. Bytes 0x9e and 0x9f are the spaces, byte 0xa0 is the 'd' in dict = {}
.
If option 'hlsearch' is on, you can see the starting whitespace with /^\s*
.
Upvotes: 1