“在缩进中使用不一致的制表符和空格" [英] "inconsistent use of tabs and spaces in indentation"

查看:112
本文介绍了“在缩进中使用不一致的制表符和空格"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Python 3.2 中创建一个应用程序,并且我一直使用制表符进行缩进,但即使是编辑器也将其中一些更改为空格,然后当我打印出在缩进中使用制表符和空格的不一致"尝试运行程序.

I'm trying to create an application in Python 3.2 and I use tabs all the time for indentation, but even the editor changes some of them into spaces and then print out "inconsistent use of tabs and spaces in indentation" when I try to run the program.

如何将空格更改为制表符?这让我疯狂.(我是编程初学者).如果我能得到一些关于我的代码的整体提示,我会很高兴,如果我犯了很多错误,我会很高兴听到.

How can I change the spaces into tabs? It's driving me crazy. (I'm a beginner in programming). I would be glad if I could get some overall tips on my code, if I have done a lot of mistakes I would be happy to hear.

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

推荐答案

不要使用制表符.

  1. 将您的编辑器设置为使用 4 个空格进行缩进.
  2. 进行搜索和替换以将所有制表符替换为 4 个空格.
  3. 确保您的编辑器设置为显示标签为 8 个空格.
  1. Set your editor to use 4 spaces for indentation.
  2. Make a search and replace to replace all tabs with 4 spaces.
  3. Make sure your editor is set to display tabs as 8 spaces.

注意:制表符有 8 个空格的原因是当无意中插入制表符时,您会立即注意到 - 例如从使用制表符而不是空格的示例代码复制和粘贴时.

Note: The reason for 8 spaces for tabs is so that you immediately notice when tabs have been inserted unintentionally - such as when copying and pasting from example code that uses tabs instead of spaces.

这篇关于“在缩进中使用不一致的制表符和空格"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆