Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Days to Target function IndentationError #103

Closed
lucyturn3r opened this issue Oct 21, 2019 · 5 comments · Fixed by #106
Closed

Days to Target function IndentationError #103

lucyturn3r opened this issue Oct 21, 2019 · 5 comments · Fixed by #106
Assignees
Labels
bug Something isn't working

Comments

@lucyturn3r
Copy link
Contributor

lucyturn3r commented Oct 21, 2019

The code below was submitted as an answer to the Days to Target function question.

def days_to_target(starting_population, target_population):
    if starting_population - target_population >= 0 :
        return 0
    else:
        keep_going = True
        day_count = 0
        current_population  = starting_population
        
        while keep_going:
            current_population *= 2
            day_count += 1
            if current_population >= target_population:
               	keep_going = False
                return day_count

When the code is run locally (I ran it in Wing), the function runs without an error being thrown and all of the test cases pass, but when it is run in codeWOF, I get the following error:

IndentationError: unindent does not match any outer indentation level on line 14
                return day_count

                ^

I have not been able to figure out why the behaviour is different when the same function is run in these two different environments.

@lucyturn3r
Copy link
Contributor Author

On diffchecker.com, there is an extra (invisible) tab-width character in the code from above which is absent in the code below (which was later submitted, and passed the tests).

def days_to_target(starting_population, target_population):
    if starting_population - target_population >= 0 :
        return 0
    else:
        keep_going = True
        day_count = 0
        current_population  = starting_population
        
        while keep_going:
            current_population *= 2
            day_count += 1
            if current_population >= target_population:
                keep_going = False
                return day_count

In this image, the code on the left is the code that gave the error, and the code on the right is the code that passed.

image

@lucyturn3r
Copy link
Contributor Author

When I convert the original code from unicode to UTF-16, the character shows up as \u0009, which is a tab. I don't understand why this tab character is invisible in both the codeWOF editor, and in Wing.

@courtneycb
Copy link
Contributor

courtneycb commented Oct 21, 2019

I'm guessing it doesn't like the mix of spaces and tabs which is causing the indentation error. If I run the code with either all spaces or all tabs it is fine. But as soon as I mix the two it throws an indentation error.

Hopefully there is a setting in CodeMirror to convert tabs to spaces or something similar.

EDIT: There is no default setting but we can try override the default tab key functionality in the CodeMirror config, see https://stackoverflow.com/questions/15183494/codemirror-tabs-to-spaces

I'll test it out to see if it works.

@courtneycb courtneycb self-assigned this Oct 21, 2019
@courtneycb
Copy link
Contributor

Overriding the tab character to input spaces works, but not if the code is copy-pasted in.

We tried parsing the users code and replacing tabs with spaces after the code was submitted but because the tab character can have a variable length we couldn't find an appropriate number of spaces to replace it with.

We then tried replacing spaces with tabs but this resulted in an indentation level of 8 spaces (the default tab size).

We are unsure why it works with this particular code in Wing. Python should give the below error if you use tabs with spaces:
image

Maybe we should add a warning if the code detects tabs?

@lucyturn3r
Copy link
Contributor Author

A warning sounds like a good idea. It could contribute as a small teaching moment, i.e. you can't use both spaces and tabs, and it doesn't require us to fix their code for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

2 participants