简体   繁体   中英

Getting the error: TypeError: 'str' object is not callable when running a particular function twice

I get the following error but only the second time I run the delete function:

`Traceback (most recent call last):
  File "English Tools.py", line 277, in <module>
    English = English_Tools()
  File "English Tools.py", line 100, in __init__
    self.delete()
TypeError: 'str' object is not callable`

Here is my code:

#Imports
import os
import sys
import time
import requests
import configparser
from os import path
from colorama import init, Fore
init()

#Color Class
class color:
  end = '\033[0m'
  bold = '\033[1m'
  underline = '\033[4m'

def clear():
  print("\n" * 50)

#Variables

end = color.end
bold = color.bold

red = Fore.RED
blue = Fore.BLUE
cyan = Fore.CYAN
green = Fore.GREEN
white = Fore.WHITE
yellow = Fore.YELLOW
purple = Fore.MAGENTA

config = configparser.ConfigParser()

title = green + bold + "English Tools"
credit = green + bold + "-Made by Jackal"

bad_info = red + "[+] " + yellow + color.bold
good_info = green + "[+] " + blue + color.bold
bad_update = red + "[!] " + yellow + color.bold
good_update = green + "[!] " + blue + color.bold

intext = blue + color.bold + ">>> " + green + end

command_words = green + bold + "[1] " + end + white + " - " + blue + "Use logophile module" + purple
command_read = green + bold + "[2] " + end + white + " - "  + blue + "Read your entrys" + purple
command_delete = green + bold + "[3] " + end + white + " - "  + blue + "Reset all data" + purple

def greeting():
  print(cyan + r"""
    ___  _____    
  .'/,-Y"     "~-.  
  l.Y             ^.           
  /\               _\_         
  i            ___/"   "\ 
  |          /"   "\   o !   
  l         ]     o !__./   
  \ _  _    \.___./    "~\  Big Homer
  X \/ \            ___./  
  ( \ ___.   _..--~~"   ~`-.  
  ` Z,--   /               \    
    \__.  (   /       ______) 
      \   l  /-----~~" /      
        Y   \          / 
        |    "x______.^ 
        |           \    
        j            Y
      """)
  print(green + "               [+] " + blue + bold + "Commands" + end + green + " [+]")
  print(purple + f"""
  ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
  █                                          █
  █  {command_words}             █
  █                                          █
  █  {command_read}                 █
  █                                          █
  █  {command_delete}                   █
  █                                          █
  █                                          █
  █                                          █
  █                                          █
  █                                          █
  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
      """)

class English_Tools:

  def __init__(self):
    while True:
      print(intext, end="")
      self.main = str(input())
      if self.main == "1":
        clear()
        self.words()
      elif self.main == "2":
        clear()
        self.read()
      elif self.main == "3":
        clear()
        self.delete()
      else:
        print(bad_update + "Invalid command")

  def words(self):
    try:
      print(good_info + "Enter the source" + end)
      print(intext, end="")
      self.source = str(input())
      clear()
      print(good_info + "Enter the page" + end)
      print(intext, end="")
      self.page = str(input())
      clear()
      print(good_info + "Enter the word" + end)
      print(intext, end="")
      self.word = str(input())
      file_exists = path.exists("data")
      if file_exists == False:
        os.mkdir("data")
        config.read("data/config.ini")
        config["word1"] = {}
        config["word2"] = {}
        config["word3"] = {}
        config["word4"] = {}
        config["logophile"] = {}
        config["word1"]["page"] = self.page
        config["word1"]["word"] = self.word
        config["word1"]["source"] = self.source
        config["word2"]["page"] = "Default"
        config["word2"]["word"] = "Default"
        config["word2"]["source"] = "Default"
        config["word3"]["page"] = "Default"
        config["word3"]["word"] = "Default"
        config["word3"]["source"] = "Default"
        config["word4"]["page"] = "Default"
        config["word4"]["word"] = "Default"
        config["word4"]["source"] = "Default"
        config["logophile"]["words_logged"] = "1"
        with open("data/config.ini", "w") as f:
          config.write(f)
          f.close()
        clear()
        greeting()
      elif file_exists == True:
        config.read("data/config.ini")
        words_logged = config.get("logophile", "words_logged")
        words_logged = int(words_logged)
        if words_logged == 1:
          config.read("data/config.ini")
          config.set("word2", "page", self.page)
          config.set("word2", "word", self.word)
          config.set("word2", "source", self.source)
          words_logged += 1
          words_logged = str(words_logged)
          config.set("logophile", "words_logged", words_logged)
          with open("data/config.ini", "w") as f:
            config.write(f)
            f.close()
        elif words_logged == 2:
          config.read("data/config.ini")
          config.set("word3", "page", self.page)
          config.set("word3", "word", self.word)
          config.set("word3", "source", self.source)
          words_logged += 1
          words_logged = str(words_logged)
          config.set("logophile", "words_logged", words_logged)
          with open("data/config.ini", "w") as f:
            config.write(f)
            f.close()
        elif words_logged == 3:
          config.read("data/config.ini")
          config.set("word4", "page", self.page)
          config.set("word4", "word", self.word)
          config.set("word4", "source", self.source)
          words_logged += 1
          words_logged = str(words_logged)
          config.set("logophile", "words_logged", words_logged)
          with open("data/config.ini", "w") as f:
            config.write(f)
            f.close()
        clear()
        greeting()
    except Exception as e:
      print(bad_update + f"Error: {e}" + end)

  def read(self):
    config.read("data/config.ini")
    words_logged = config.get("logophile", "words_logged")
    words_logged = int(words_logged)
    word1_word = config.get("word1", "word")
    word1_source = config.get("word1", "source")
    word1_page = config.get("word1", "page")
    word2_word = config.get("word2", "word")
    word2_source = config.get("word2", "source")
    word2_page = config.get("word2", "page")
    word3_word = config.get("word3", "word")
    word3_source = config.get("word3", "source")
    word3_page = config.get("word3", "page")
    word4_word = config.get("word4", "word")
    word4_source = config.get("word4", "source")
    word4_page = config.get("word4", "page")
    if words_logged == 1:
      print(blue + "Word: " + green + f"{word1_word}")
      print(blue + "Source: " + green + f"{word1_source}")
      print(blue + "Page: " + green + f"{word1_page}")
      print(good_info + "Enter anything to exit" + end)
      print(intext, end="")
      self.exit_read = str(input().lower)
      clear()
      greeting()
      return
    elif words_logged == 2:
      print(blue + "Word: " + green + f"{word1_word}")
      print(blue + "Source: " + green + f"{word1_source}")
      print(blue + "Page: " + green + f"{word1_page}\n")
      print(blue + "Word: " + green + f"{word2_word}")
      print(blue + "Source: " + green + f"{word2_source}")
      print(blue + "Page: " + green + f"{word2_page}")
      print(good_info + "Enter anything to exit" + end)
      print(intext, end="")
      self.exit_read = str(input().lower)
      clear()
      greeting()
      return
    elif words_logged == 3:
      print(blue + "Word: " + green + f"{word1_word}")
      print(blue + "Source: " + green + f"{word1_source}")
      print(blue + "Page: " + green + f"{word1_page}\n")
      print(blue + "Word: " + green + f"{word2_word}")
      print(blue + "Source: " + green + f"{word2_source}")
      print(blue + "Page: " + green + f"{word2_page}\n")
      print(blue + "Word: " + green + f"{word3_word}")
      print(blue + "Source: " + green + f"{word3_source}")
      print(blue + "Page: " + green + f"{word3_page}")
      print(good_info + "Enter anything to exit" + end)
      print(intext, end="")
      self.exit_read = str(input().lower)
      clear()
      greeting()
      return
    elif words_logged == 4:
      print(blue + "Word: " + green + f"{word1_word}")
      print(blue + "Source: " + green + f"{word1_source}")
      print(blue + "Page: " + green + f"{word1_page}\n")
      print(blue + "Word: " + green + f"{word2_word}")
      print(blue + "Source: " + green + f"{word2_source}")
      print(blue + "Page: " + green + f"{word2_page}\n")
      print(blue + "Word: " + green + f"{word3_word}")
      print(blue + "Source: " + green + f"{word3_source}")
      print(blue + "Page: " + green + f"{word3_page}\n")
      print(blue + "Word: " + green + f"{word4_word}")
      print(blue + "Source: " + green + f"{word4_source}")
      print(blue + "Page: " + green + f"{word4_page}")
      print(good_info + "Enter anything to exit" + end)
      print(intext, end="")
      self.exit_read = str(input().lower)
      clear()
      greeting()
      return

  def delete(self):
    print(good_info + "Are you sure you would like to reset all data? | (y/n) |" + end)
    print(intext, end="")
    self.delete = str(input().lower())
    if self.delete == "y":
      os.remove("data/config.ini")
      os.rmdir("data")
      print(good_update + "Deleted" + end)
      greeting()
      return
    else:
      clear()
      greeting()
      return

greeting()
English = English_Tools()

I can run the delete function perfectly the first time but if I try to run it again in the same session it throws the error. Anyone know how to fix this? I can run all of the other functions multiple times but it is just the delete function that is giving me trouble.

self.delete = str(input().lower())

You're taking the name that held the delete function ( self.delete ) and overwriting it , turning it into a string that's holding the user's input. The easiest fix by far is using a different name for that variable - perhaps want_to_delete or something similar?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM