首页 文章

unindent与任何外部缩进级别都不匹配,为什么会发生这种情况

提问于
浏览
0

这是我的代码:

import urllib
import re
import requests
import httplib
import socket
from colorama import *
from time import gmtime, strftime
  def hell():
    hcon = raw_input(Fore.RED + Style.BRIGHT + "Website: ")
    r = requests.get("http://" + hcon + "/phpmyadmin")
    r2 = requests.get("http://" + hcon + "/pma")
    if r.status_code == 404:
     print(strftime("[%H:%M:%S]", gmtime()) + " /phpmyadmin/ not found!") 
    elif r: 
     print(strftime("[%H:%M:%S]", gmtime()) + " /phpmyadmin/ successfully found! %s /phpmyadmin") % (hcon)
    elif r2:
     print(strftime("[%H:%M:%S]", gmtime()) + " /phpmyadmin/ successfully found! %s /pma") % (hcon)
    else:
     print(strftime("[%H:%M:%S]", gmtime()) + " nothing found, sorry! try another website.")

hell()

正如您所看到的,如果所选网站上存在页面/ phpmyadmin或/ pma,则会输出已成功找到的页面,如果不是,则会显示“找不到任何内容等等” .

但是我得到了这个错误:unindent与外部缩进级别不匹配,我以前从未在我以前的任何python脚本中出现过这个错误 .

我尽可能多地修复了缩进,但我认为不会解决它,有人可以帮忙吗?

5 回答

  • 0

    在您的情况下, hell 方法定义必须位于根缩进级别 .

  • 0

    def hell()以及下面的所有内容都应该减少1个缩进 .

  • 0

    复制自评论,因为现在这是答案 .

    所以你粘贴的代码是不完整的,所以我不能直接修复你得到的错误 .

    当你有一行用X空格缩进,然后有些用X Y Z空格缩进,最后用X Y空格缩进时,你的错误通常会显示出来 . 无论什么时候,Python都要求你回到以前的那个级别 . 例如:

    def foo():
        print "hi!"
      return 42
    

    请注意,return语句的缩进是2个空格,但没有前一行缩进2个空格 . 这是不允许的,它需要缩进到4个空格,或者print语句移回到2个空格中 .

  • 0

    你有混合空格和标签 . 尝试在记事本中打开代码或具有8个空格缩进级别的代码,看看它的外观 . 它肯定与你发布的内容不符,或者你会得到“意外缩进”错误而不是无意义的问题 .

  • 2

    您需要修复 def hell() 的缩进:

    import urllib
    import re
    import requests
    import httplib
    import socket
    from colorama import *
    from time import gmtime, strftime
    
    def hell():
        hcon = raw_input(Fore.RED + Style.BRIGHT + "Website: ")
        r = requests.get("http://" + hcon + "/phpmyadmin")
        r2 = requests.get("http://" + hcon + "/pma")
        if r.status_code == 404:
            print(strftime("[%H:%M:%S]", gmtime()) + " /phpmyadmin/ not found!") 
        elif r: 
            print(strftime("[%H:%M:%S]", gmtime()) + " /phpmyadmin/ successfully found! %s /phpmyadmin") % (hcon)
        elif r2:
            print(strftime("[%H:%M:%S]", gmtime()) + " /phpmyadmin/ successfully found! %s /pma") % (hcon)
        else:
            print(strftime("[%H:%M:%S]", gmtime()) + " nothing found, sorry! try another website.")
    
    hell()
    

相关问题