【问题标题】:Name is not identified when returning into a function python [duplicate]返回到函数python时未识别名称[重复]
【发布时间】:2019-12-31 22:06:17
【问题描述】:

这是我的代码:

def checking():

  response2 = requests.get('https://www.nitrxgen.net/md5db/' + str(word[1])).text  

  if response2:
   print(Fore.GREEN + "[" + Fore.WHITE + strftime("%H:%M:%S")+ Fore.GREEN + "]" + Fore.WHITE + " |" + Fore.GREEN + " ■ " + Fore.WHITE +
    "Hash Found: " + Fore.GREEN + response2 + Style.RESET_ALL + "\n", end='')

  else:
   print(Fore.RED + "[" + Fore.WHITE + strftime("%H:%M:%S")+ Fore.RED + "]" + Fore.WHITE + " |" + Fore.RED + " ■ " + Fore.WHITE +
    "Hash Not Found" + Style.RESET_ALL + "\n", end='')

def Mutiple():

  Tk().withdraw() 
  filename = askopenfilename(title='Choose a File', filetypes=[("Text Files", "*.txt")])
  clear = lambda: os.system('cls')
  clear() 
  with open(filename, "r", encoding="utf8") as file:  
    for line in file:
        word = line.strip()
        word = word.split(":")
        return checking()

错误码是

Traceback (most recent call last):   
  File "C:\Users\tosun\OneDrive\Desktop\Hashkiller\Hashkiller.py", line 111, in <module>
    Mutiple()   
  File "C:\Users\tosun\OneDrive\Desktop\Hashkiller\Hashkiller.py", line 40, in Mutiple
    return checking()   
  File "C:\Users\tosun\OneDrive\Desktop\Hashkiller\Hashkiller.py", line 18,  in checking
    response2 = requests.get('https://www.nitrxgen.net/md5db/' + str(word[1])).text 
NameError: name 'word' is not defined

【问题讨论】:

  • return 语句真的应该在for 循环内吗?它只会处理文件的第一行。
  • 另外,checking() 不会返回任何内容。 return checking() 应该返回什么?
  • 给定的“重复”对于这个问题来说有点矫枉过正,但我​​希望它能解决您的问题,并且您可能还会遇到更多问题。

标签: python function


【解决方案1】:

wordMutiple() 中的局部变量。如果你想在checking()中使用它,你应该把它作为参数传递。

def checking(word):

  response2 = requests.get('https://www.nitrxgen.net/md5db/' + str(word[1])).text  

  if response2:
   print(Fore.GREEN + "[" + Fore.WHITE + strftime("%H:%M:%S")+ Fore.GREEN + "]" + Fore.WHITE + " |" + Fore.GREEN + " ■ " + Fore.WHITE +
    "Hash Found: " + Fore.GREEN + response2 + Style.RESET_ALL + "\n", end='')

  else:
   print(Fore.RED + "[" + Fore.WHITE + strftime("%H:%M:%S")+ Fore.RED + "]" + Fore.WHITE + " |" + Fore.RED + " ■ " + Fore.WHITE +
    "Hash Not Found" + Style.RESET_ALL + "\n", end='')

def Mutiple():

  Tk().withdraw() 
  filename = askopenfilename(title='Choose a File', filetypes=[("Text Files", "*.txt")])
  clear = lambda: os.system('cls')
  clear() 
  with open(filename, "r", encoding="utf8") as file:  
    for line in file:
        word = line.strip()
        word = word.split(":")
        return checking(word)

【讨论】:

    猜你喜欢
    • 2023-02-11
    • 2020-03-03
    • 2018-05-09
    • 2020-09-17
    • 2012-02-12
    • 2021-07-19
    • 2020-10-05
    • 2019-07-03
    • 2016-09-02
    相关资源
    最近更新 更多