-2

Hi when i am copying this complete snippet in python shell it is giving indentation error


import heapq    
dict = {4: 'four', 1 : 'one', 3: 'third', 2: 'two', 5:'five'}
h = []
for value in dict:
    heapq.heappush(h, value)

for i in range(len(h)):
    a = heapq.heappop(h)
    print a,'  ',dict[a]

but if i copy first block

import heapq    
dict = {4: 'four', 1 : 'one', 3: 'third', 2: 'two', 5:'five'}
h = []
for value in dict:
    heapq.heappush(h, value)

hit enter and then copy second block

for i in range(len(h)):
    a = heapq.heappop(h)
    print a,'  ',dict[a]

hit enter and it works fine

please guide where indentation problem is happening.

commanche
  • 3
  • 2

1 Answers1

2

When you paste in the sequence:

for value in dict:
    heapq.heappush(h, value)

for i in range(len(h)):

Auto-indentation results in:

for value in dict:
    heapq.heappush(h, value)

    for i in range(len(h)):

Hence your problem.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73