微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我正在尝试在 if 语句中调用已定义的函数,但出现错误

如何解决我正在尝试在 if 语句中调用已定义的函数,但出现错误

我正在使用 python 来运行我的代码,我已经尝试了所有我能想到的调试,但我仍然遇到错误

SubOrSuper = input("\nWould you like to translate a \033[1msubscript\033[0m,or a \033[1msuperscript?\033[0m\n")

  if SubOrSuper == "subscript" :
    TransToSuper = input ("\nInput your subscript here: ")
        TTSup()

#Subscript to Superscript translating process
def TTSup() :
    if TransToSuper == "H" :
        Hsub()

错误

File "main.py",line 141
  TTSup()
        ^
TabError: inconsistent use of tabs and spaces  in indentation

解决方法

您的代码:

SubOrSuper = input("\nWould you like to translate a \033[1msubscript\033[0m,or a \033[1msuperscript?\033[0m\n")

  if SubOrSuper == "subscript" : #Here you have two extra spaces that should not be. Your if has a wrong indent,and should not have at all
    TransToSuper = input ("\nInput your subscript here: ")
        TTSup() #There is no need to indent here,you did above after the If

#Subscript to Superscript translating process
def TTSup() :
    if TransToSuper == "H" :
        Hsub() #Here you did indent correctly

更正的代码:

SubOrSuper = input("\nWould you like to translate a \033[1msubscript\033[0m,or a \033[1msuperscript?\033[0m\n")

if SubOrSuper == "subscript" :
    TransToSuper = input ("\nInput your subscript here: ")
    TTSup()

#Subscript to Superscript translating process
def TTSup() :
    if TransToSuper == "H" :
        Hsub()

正如评论中所说,当错误消息说:

File "main.py",line 141
  TTSup()
        ^
TabError: inconsistent use of tabs and spaces  in indentation

这意味着:inconsistent use of tabs and spaces in indentation 处有一个 line 141

所以你需要在这条线上工作。如果您不知道,请将其删除,看看它是否有效。如果它确实有效,请以您能想到的任何不同方式重新添加它(再次,如果您不理解/无法在网上找到任何答案)。当它最终奏效时,您应该通过从一开始就检查差异来理解原因。

,

您能否尝试在 TTSup() 函数调用之前删除该选项卡。你会很好的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。