Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6700225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:47:22+00:00 2026-05-26T06:47:22+00:00

I’m trying to run the following code in python3, but it has been written

  • 0

I’m trying to run the following code in python3, but it has been written for I’m pretty sure python2:

f = open(filename, 'r')
self.lines = f.readlines()
f.close()
if self.lines[-1] != "\n" :
    self.lines.append("\n")

But I’m getting the following error:

  File "randline.py", line 32
    if self.lines[-1] != "\n" :
                              ^
TabError: inconsistent use of tabs and spaces in indentation

Can you help me figure out the correct syntax?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:47:22+00:00Added an answer on May 26, 2026 at 6:47 am

    Python 2 allows you to mix spaces and tabs. so you can have indentation like:

    def foo():
    [this is a tab it counts like eight spaces             ]for each in range(5):
    [this is a tab it counts like eight spaces             ][space][space]print(each)
    [space][space][space][space][space][space][space][space]print("Done!")
    

    Line 2 and line 4 will in Python 2 have the same indentation level, but line 2 will do it with a tab, and line 4 will do it with spaces. Printed to the console, it will look like this:

    def foo()
            for each in range(5):
              print(5)
            print("Done!")
    

    But most editors allow you to set how many spaces a tab should be. Set it to four, and you get this:

    def foo()
        for each in range(5):
          print(5)
            print("Done!")
    

    The indentation is still the same, but now it looks like the indentation is wrong!

    Python 3 therefore do not allow the same indentation level (ie line 2 and 4) to be indented in different ways. You can still mix tabs and spaces, but not in the same indentation level. This means that

    def foo():
    [this is a tab it counts like eight spaces             ]for each in range(5):
    [this is a tab it counts like eight spaces             ][space][space]print(each)
    [this is a tab it counts like eight spaces             ]print("Done!")
    

    will work, and so will

    def foo():
    [this is a tab it counts like eight spaces             ]for each in range(5):
    [space][space][space][space][space][space][space][space][space][space]print(each)
    [this is a tab it counts like eight spaces             ]print("Done!")
    

    The only way to make that the only way you can make the indentation look weird is of you set a tab to be more than eight spaces, and then the indentation not only looks obviously incorrect, you’ll notice that a tab will indent 12 spaces (in the below example) so you realize you insert a tab, and not four spaces.

    def foo():
                for each in range(5):
              print(each)
                print("Done!")
    

    Of course, the solution to all your problems is as written in the comments, to never use tabs. I’m not sure why Python 3 still allows tabs at all, there is no good reason for that, really.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.