繁体   English   中英

经过如此多的故障排除后,我不断收到回溯和 str 回调错误,但我似乎无法修复它。 Python 通讯录

[英]I keep getting traceback and str callback errors after so much troubleshooting and I can't seem to fix it. Python contacts book

我已将字典保存在一个单独的文档中,其中包含名为 directory.txt 的所有正确语法。 该代码用于使用字典的通讯录/目录。 我不确定我应该在调用它们之前还是之后定义函数,但我已经尝试了这两种方法,但它似乎仍然不起作用。

file = open("directory.txt","r+")
contacts = file.read()
def new():
    print("Please enter the name of the new contact.")
    name = input()
    print("Please enter the number of the new contact.")
    num = input()
    contacts.update({name:num})
    print("Contact has successfully been added.")
def view():
    for keys, values in contacts():
        print("\n*************************************************")
        print("Name:",keys)
        print("Number:",values)
        print("*************************************************\n")
def edit():
    for keys, values in contacts():
        print("\n*************************************************")
        print("Name:",keys)
        print("Number:",values)
        print("*************************************************\n")
    print("Type the name of the contact you would like to edit.")
    global name_two
    name_two = input()
    print("If you would like to edit the number, type 'number'.\nIf you would like to edit the name, type 'name'.\nIf you would like to delete this contact, type 'delete'.")
    edit = input().lower()
    if edit == "number":
        num_edit()
    elif edit == "name":
        name_edit()
    elif edit == "delete":
        delete()
def num_edit():
    print("What would you like to change the number to?")
    num_two = input()
    contacts[name_two] = num_two
    print("Contact successfully changed.")
def name_edit():
    num_save = contacts[name_two]
    del contacts[name_two]
    print("Enter the new name for the contact.")
    name_new = input()
    contacts.update({name_new:num_save})
    print("Contact successfully changed.")
def delete():
    del contacts[name_two]
    print("Contact successfully deleted.")

print("Welcome to the Contacts Book.")
print("*****************************\n")
print("If you would like to make a new contact, type 'new'.\nIf you would like to view your contacts, type 'view'.\n If you would like to edit your contacts, type 'edit'.\nIf you would like to exit, type 'exit'")
mode = input().lower()
if mode == "new":
    new()
elif mode == "view":
    view()
elif mode == "edit":
    edit()
else:
    print("Goodbye.")

这是当我键入“view”作为第一个输入时的错误消息:

File "c:\Users\brain\Downloads\contacts.py", line 56, in <module>
    view()
  File "c:\Users\brain\Downloads\contacts.py", line 11, in view
    for keys, values in contacts():
TypeError: 'str' object is not callable

我认为您的代码中有一些名为str 的变量,这导致了这个问题。 这称为隐藏内置函数,您可以尝试通过在项目文件中搜索来找到该变量并将其更改为其他名称并解决它。

我相信你在接触之后只有一组不必要的括号(据我所知,它是一个 var 不是一个函数),所以只需删除它们,你应该会很好。 编辑:gimix 回复比我在这里写的要准确得多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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