首页 > 解决方案 > 为什么我的函数中会出现这个 IndentationError?

问题描述

我正在用python开发一个函数。但是我收到此错误 IndentationError: unindent does not match any external indentation level

我应该怎么办?这是我的代码

def customerdetails():
      Firs_tname = input("Enter your First name:")
      Last_name = input("Enter your last name:")
      Age = input("Age:")
      Address =input("Enter your address:")
      Postcode = input("Enter your Postcode:")
      Email = input("Email:")
      Phone = int(input("Phone Number:"))
    customerdetails()

我会一次又一次地使用这个功能

标签: pythonpython-3.xvisual-studio-code

解决方案


customerdetails()在它试图调用的同一个函数中。您不能以这种方式从自身内部调用函数。要使此代码正常工作,customerdetails()不得缩进。这是正确的代码:

def customerdetails():
      Firs_tname = input("Enter your First name:")
      Last_name = input("Enter your last name:")
      Age = input("Age:")
      Address =input("Enter your address:")
      Postcode = input("Enter your Postcode:")
      Email = input("Email:")
      Phone = int(input("Phone Number:"))
customerdetails()

此外,对于缩进,您应该使用制表符,因为它们比空格更一致(不是每个人都知道 4 个空格是正确的缩进),尤其是在像 VSC 这样的成熟文本编辑器中。有些人更喜欢空间,但我个人认为它们只会让事情变得不必要地困难。


推荐阅读