我试图在Python 3.2中创建一个应用程序,我一直使用制表符进行缩进,但即使是编辑器也将其中一些更改为空格,然后在我试图运行程序时打印出“在缩进中使用制表符和空格不一致”。

如何将空格改为制表符?快把我逼疯了。

import random

attraktioner = ["frittfall","bergodalbana","spökhuset"]


class Nojesfalt:
    def __init__(self, attraktion):
        self.val = attraktion
        self.langd = 0
        self.alder = 0


#längdgräns för fritt fall
    def langdgrans(self):
        print("")
        self.langd = int(input("Hur lång är du i cm? "))
        if self.langd < 140:
            print("tyvärr, du är för kort, prova något annat")
            return 0
        elif self.langd >= 140:
            print("håll dig hatten, nu åker vi!")
            print(" ")
            return 1

#åldersgräns för spökhuset
    def aldersgrans(self):
        print("")
        self.alder = int(input("Hur gammal är du? "))
        if self.alder < 10:
            print("tyvärr, du är för ung, prova något annat")
            return 0
        elif self.alder >= 10:
            print("Gå in om du törs!")
            print(" ")
            return 1


#åker attraktion frittfall lr bergodalbana
        def aka(self):
                print("")
        print(self.val)
        tal = random.randint(0,100)
        if tal < 20:
            print("åkturen gick åt skogen, bättre lycka nästa gång")
        elif tal >= 20:
            print("jabbadabbbadoooooooo")
            return 1

#går i spökhuset
        def aka1(self):
                print("")
        print(self.val)
        tal = random.randint(0,100)
        if tal < 20:
            print("du är omringad av spöken och kan inte fortsätta")            return 0
        elif tal >= 20:
            print("Buhuuuuuu, buuuhuuuu")
            return 1

#programkod
print("Välkommen till nöjesfältet, vad vill du göra?")
print(" ")

while 1:
    vald_attr = input("Vad vill du göra?\n1. frittfall\n2. bergodalbana\n3. spökhuset\n4. Avsluta\n")
    if vald_attr == "1":
        val = Nojesfalt(attraktioner[0])
        if val.langdgrans() == 1:
            val.aka()
    elif vald_attr == "2":
        val = Nojesfalt(attraktioner[1])
        val.aka()
    elif vald_attr == "3":
        val = Nojesfalt(attraktioner[2])
        if val.aldersgrans() == 1:
            val.aka1()
    elif vald_attr == "4":
        break

当前回答

虽然最初的问题是关于自己编写的代码,但搜索引擎在搜索标题字符串时引导到这里。当试图使用已经存在的库或工具时,很可能会收到错误消息。

对于那些试图使用别人代码的人来说;根据Python中的Tab Error,这是一个python2 vs. python3的问题(这个答案也参考了PEP8样式指南的相关部分)。

其他回答

下面的技巧对我很管用:

将代码复制并粘贴到记事本中。 然后从记事本再次选择所有和复制的代码 粘贴到我的views.py 在views.py中选择所有新粘贴的代码,并通过按shift+tab键从键盘上删除所有制表符 现在再次使用tab键来使用适当的缩进

一般来说,人们更喜欢用空格缩进。它在编辑器之间更加一致,从而减少了这种类型的不匹配。但是,您可以使用制表符缩进。这是你的选择;但是,您应该意识到每个制表符8个空格的标准有点宽。

关于你的问题,很可能是你的编辑搞砸了。将制表符转换为空格实际上依赖于编辑器。

例如,在Emacs上,您可以调用方法“untabify”。

在命令行中,您可以使用sed行(根据您的喜好调整空格的数量):

   sed -e 's;\t;       ;' < yourFile.py > yourNedFile.py

There was a duplicate of this question from here but I thought I would offer a view to do with modern editors and the vast array of features they offer. With python code, anything that needs to be intented in a .py file, needs to either all be intented using the tab key, or by spaces. Convention is to use four spaces for an indentation. Most editors have the ability to visually show on the editor whether the code is being indented with spaces or tabs, which helps greatly for debugging. For example, with atom, going to preferences and then editor you can see the following two options:

然后,如果你的代码使用空格,你会在你的代码缩进的地方看到小点:

如果它使用制表符缩进,你会看到类似这样的东西:

Now if you noticed, you can see that when using tabs, there are more errors/warnings on the left, this is because of something called pep8 pep8 documentation, which is basically a uniform style guide for python, so that all developers mostly code to the same standard and appearance, which helps when trying to understand other peoples code, it is in pep8 which favors the use of spaces to indent rather than tabs. And we can see the editor showing that there is a warning relating to pep8 warning code W191,

我希望以上所有内容都能帮助您了解您所遇到的问题的性质,以及如何在将来预防它。

使用Vim编辑器解决这个问题

Open terminal (Ctrl + Alt + T). Go to the directory where the file is located (cd <path_to_your_directory>). Ex: cd /home/vineeshvs/work. Open the file in Vim (vim <file_name>). Ex: vim myfile.txt . [Optional step] Enable search keyword highlighting in Vim (ESC :set hlsearch) Go to the line where you have this problem (ESC :<line_number>). Ex: :53 in Vim editor after pressing ESC button once. Replace tabs using the required number of spaces in Vim (:.,$s/\t/<give_as_many_spaces_as_you_want_to_replace_tab>/gc). Ex: Tab will be replaced with four spaces using the following command: :.,$s/\t/ /gc after pressing ESC button once). This process is interactive. You may give y to replace the tab with spaces and n to skip a particular replacement. Press ESC when you are done with the required replacements.

我也犯了同样的错误。我必须向现有的*.py文件中添加几行代码。在notepad++中,它不起作用。

在添加代码行并保存后,我得到了相同的错误。当我在PyCharm中打开相同的文件并添加这些行时,错误消失了。