【问题标题】:'expected an indent block''预期缩进块'
【发布时间】:2013-09-14 21:10:23
【问题描述】:

我在冒号后得到“预期的 ident 块”:def StartGame(username): 对于该行的其余部分,它会出现红色,并且是编程新手,我不确定自己做错了什么。提前谢谢你:)

def StartGame(username):
  print("This game is all about Trading and Adventuring.")
  time.sleep(0.51)
  print("This is what is in your inventory: %s" % Inventory)
  time.sleep(1)
  print("As you go around, you will start to lose hunger. You will need to buy food along the way.")
  time.sleep(1)
  print("You have %s Health Points (HP)" % UserHunger)
  TO.SetRandName()

【问题讨论】:

  • 4 个空格的缩进,你只做了 1 或 2 个,这就是为什么当它告诉你它需要一个缩进时,那是因为你需要有 4 个空格的缩进
  • @Infamouslyuseless:Python 实际上并不关心你的缩进有多大。您甚至可以在单个文件中混合和匹配缩进大小,尽管这样做很糟糕。
  • @user2357112 我知道,但通常人们会做 4 个空格,或者按 Tab,这也是一样的,因为如果你明白我的意思,它看起来更简洁,更容易理解?
  • @Infamouslyuseless:人们通常会这样做,但您的评论说如果您使用不同的缩进大小,Python 会抱怨。 Python 不在乎。 (此外,除非您明确指示您的文本编辑器插入 4 个空格而不是一个制表符,否则按 Tab 并不等于 4 个空格。)
  • @user2357112 与 eclipse pydev 很好,我认为 tab 会自动完成,因为我没有告诉它,当我点击 tab 时它会自动执行 4 个空格...

标签: python block indentation


【解决方案1】:

看起来您正在混合制表符和空格。使用 4 个空格进行缩进:

def StartGame(username):
    print("This game is all about Trading and Adventuring.")
    time.sleep(0.51)
    print("This is what is in your inventory: %s" % Inventory)
    time.sleep(1)
    print("As you go around, you will start to lose hunger. You will need to buy food along the way.")
    time.sleep(1)
    print("You have %s Health Points (HP)" % UserHunger)
    TO.SetRandName()

另见PEP-8 indentation paragraph

【讨论】:

    猜你喜欢
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多