Python Visual Studio Question

Hello,

So I’ve been trying the projects in the scientific computing with python section. And lately when I have run my code on visualstudio it comes up with a strange error I can’t seem to understand, and I was wondering if someone from here can provide some insight.

So the problem is I’ve deleted a line of code, but when I press run it still thinks it is there and flags up as an error.
The error:

line 43
    return how_many/num_experiments
    ^
IndentationError: expected an indented block

C:\Users\afiaz\Test>


But you’ll notice I don’t even have that line in my code anymore, mainly because I was trying to figure out why it was happening:

def experiment(hat, expected_balls, num_balls_drawn,num_experiments):
    #convert expected_balls to list
    expected= sorted([])
    value= 0
    for key in expected_balls:
        value = 0
        while value<expected_balls[key]:
            value+=1
            expected.append(key)
    how_many = 0 
    for experiment in range(num_experiments):
        hatcopy = copy.deepcopy(hat)
        drawn = hatcopy.draw(num_balls_drawn)
        check = [] 
        for draw in drawn:
            if draw in expected:
                check.append(drawn.remove(draw))
        print(check)

Hi,

Could you please post a snapshot of the entire window, or maybe the entire file, might be easier to see what is going on. I also notice you don’t have 43 lines. Could it be you have left something far in the bottom of the file.

Python is sensitive to indentation. This error indicates that there is a line that is not properly indented. The weird thing is the line number.

Silly question, after you deleted the line did you save the file. This has thrown me before :slight_smile:

2 Likes

So this is the entire bit of code I have in this file:

import copy
import random

class Hat:
    def __init__(self, **kwargs): 
        self.spec = kwargs
        self.contents= [] #convert dic to string contents
        value= 0
        for key in kwargs:
            value = 0
            while value<kwargs[key]:
                value+=1
                self.contents.append(key)

    def draw(self, number): #draw number of balls
        removed = []
        amount = len(self.contents)
        if number > amount:
            removed = self.contents
        else:
            for x in range (number):
                ran_choice=random.randrange(0, len(self.contents))
                removed.append(self.contents.pop(ran_choice))
        return removed

def experiment(hat, expected_balls, num_balls_drawn,num_experiments):
    #convert expected_balls to list
    expected= sorted([])
    value= 0
    for key in expected_balls:
        value = 0
        while value<expected_balls[key]:
            value+=1
            expected.append(key)
    how_many = 0 
    for experiment in range(num_experiments):
        hatcopy = copy.deepcopy(hat)
        drawn = hatcopy.draw(num_balls_drawn)
        check = [] 
        for draw in drawn:


    return how_many/num_experiments





hat3 = Hat(red=5,blue=2)


print(experiment(hat3, {"blue":2,"red":1}, 4, 10))

and the output:

>C:/Users/afiaz/AppData/Local/Programs/Python/Python38/python.exe c:/Users/afiaz/Test/probability.py
  File "c:/Users/afiaz/Test/probability.py", line 43
    return how_many/num_experiments
    ^
IndentationError: expected an indented block

To solve this I have opened up a new file, copy and pasted it into there and it seems to work. However, I’d like to get to the bottom of why it’s happening in the first place.

That error always means you had an indentation error somewhere in your code near the line where that error was triggered. A line was indented too many or too few spaces.

Yes, as JeremyLT mentioned, that was due to the indentation, it is likely that the copy to a different file could have corrected the indentation

I see on the below for that you dont have anything below. Python is waiting for a command in the for, so it might be thinking that the return statement is meant for the for. If you want to have an empty loop/conditional you could use the command pass

4 Likes

I don’t think it is that in this case as I have run the code successfully before, nothing has changed in a majority of the code since then.

Maybe it’ s a visual code problem rather than a code one? I’ve checked the indentation on the two files and they seem to be the same.

Nope. It’s an indentation problem. It’s really easy to mess up indentation and when you mess up indentation you get that error message. Copy-pasting in visual studio will often trigger the auto-formatter, which is typically pretty good of fixing indentation issues.


Edit: Sometimes the indentation issue is a mix of tabs and spaces. That sort of thing can be hard to see visually but copy-pasting into the editor again is an easy way to fix mixed indentation types.

1 Like

If an error pops up about indentation with Python in VSC, I would trust the error and if you can’t figure out on your own how you wrote it incorrectly, to learn about why the block needed the indent changed. It can make a HUGE difference doing return statements.

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