首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python显示存在的字典的名称错误。

Python显示存在的字典的名称错误。
EN

Stack Overflow用户
提问于 2021-10-02 17:03:35
回答 1查看 64关注 0票数 0

在我的学校项目中,我制作了一个图书馆管理系统,在这个系统中,分配给学生的图书ID应该在另一本词典(Bkd)的钥匙中(它存储图书详细信息,钥匙是图书ID)。学生记录在文件library.dat中,图书记录在文件bookrec.dat中,学生记录以字典(Rec)形式存在,图书详细信息现在在字典(Bkd)中,当我试图使用if语句检查用户输入的图书id是否在bkd.keys()中时,程序会给出一个名称错误。有什么解决办法吗?

代码语言:javascript
复制
def crt_lib_rec():
    g=open('bookrec.dat','rb')
    f=open('library.dat','ab')
    rec={}
    rno=int(input('Enter Rno: '))
    nm=input('Enter name: ')
    gd=int(input("Enter the Grade of the student: "))
    div=input("Enter the Class of the student: ")
    bookid=int(input("Enter the Book ID: "))
    retdate=input("Enter the date of return: ")
    if bookid in bkd.keys():
        rec[rno]={'Name':nm,'Grade':gd,'Division':div,'Book ID':bookid,'Return Date':retdate}
        p.dump(rec,f)
        print("Record Created Successfully..")
    else:
        print("Enter a valid Book ID..")

字典bkd是在另一个函数crtbookrec()中创建的

下面是crtbookrec:

代码语言:javascript
复制
def crt_book_rec():
    g=open('bookrec.dat','ab')
    bkd={}
    bkid=int(input('Enter Book ID: '))
    bknme=input('Enter Book Name: ')
    authnme=input("Enter Author Name: ")
    bkd[bkid]={'Book Name':bknme,'Author Name':authnme}
    p.dump(bkd,g)
    g.close()

错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\Users\kesha\Desktop\Project\Proj copy for testing\proj test.py", line 410, in <module>
    menufn()
  File "C:\Users\kesha\Desktop\Project\Proj copy for testing\proj test.py", line 350, in menufn
    crtlibrec()
  File "C:\Users\kesha\Desktop\Project\Proj copy for testing\proj test.py", line 19, in crtlibrec
    if bookid in bkd.keys():
NameError: name 'bkd' is not defined

调用函数的菜单:

代码语言:javascript
复制
def menufn():
    menu1='1.Student Details\n2.Book Details\n3.Exit'
    while True:
        print('='*90)
        print("\n\t\t\t    ABC Public School\n\n\t\t\t     Kottayam,Kerala\n\n\t\t\t    LIBRARY MANAGEMENT\n")
        print('='*90)
        print(menu1)
        print('='*90)
        opt1=input("Enter your choice(1/2/3): ")
        if opt1=='1':
            menu2='1.Create Student Record\n2.Display Student Records\n3.Modify Student Records\n4.Modify Book Issued\n5.Modify Return Date\n6.Search Student Records\n7.Delete Student Record\n8.Exit The Program'
            ch2='y'
            while True:
                print('='*90)
                print("\n\t\t\tSTUDENT DETAILS PROGRAM\n")
                print(menu2,"\n")
                print('='*90)
                opt2=input('Enter your choice: ')
                if opt2=='1':
                    #crtlibrec is called here
                    print('-'*10,'Creating Record','-'*10)
                    crt_lib_rec(bkd)
                elif opt2=='2':
                    print('-'*10,'Displaying Records','-'*10)
                    display_studet()
                elif opt2=='3':
                    print('-'*10,'Modifying Student Records','-'*10)
                    modify_stdet()
                elif opt2=='4':
                    print('-'*10,'Modifying Book Issued','-'*10)
                    modify_book_issd()
                elif opt2=='5':
                    print('-'*10,'Modifying Return Date','-'*10)
                    modify_ret_date()
                elif opt2=='6':
                    print('-'*10,'Searching Records','-'*10)
                    Search_stud()
                elif opt2=='7':
                    print('-'*10,'Deleting Student Record','-'*10)
                    Delete_std_rec()
                
                elif opt2=='8':
                    print('Exiting....')
                    break
                else:
                    print('Enter valid input')
                
        elif opt1=="2":
            menu3='1.Create Book Record\n2.Display Book Record\n3.Modify Book Records\n4.Search Book Records\n5.Delete Book Record\n6.Exit The Program'
            
            while True:
                print('='*90)
                print("\n\t\t\tBOOK DETAILS PROGRAM\n")
                print(menu3)
                print('='*90)
                opt3=input('Enter your choice: ')
                if opt3=='1':
                    #crtbookrec is called here
                    print('-'*10,'Creating Book Record','-'*10)
                    crt_book_rec()
                elif opt3=='2':
                    print('-'*10,'Displaying Book Records','-'*10)
                    display_book_rec()
                elif opt3=='3':
                    print('-'*10,'Modifying Book Records','-'*10)
                    modify_book_rec()
                elif opt3=='4':
                    print('-'*10,'Searching Book Records','-'*10)
                    search_book_rec()
                elif opt3=='5':
                    print('-'*10,'Deleting Book Record','-'*10)
                    delete_book_rec()
                elif opt3=='6':
                    print('Exiting....')
                    break
                else:
                    print('Enter valid input')
        elif opt1=="3":
            print("Thank You for using the Library Management Program")
            break
        else:
            print("Invalid Choice..")
menufn()

如果代码注释不好或者写得不好,我很抱歉,我对python很陌生

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-02 17:24:53

代码骨架应该如下所示

代码语言:javascript
复制
def crtlibrec(bkd):
    # do something with bkd

def crtbookrec():
  return bkd

def main():
  bkd = crtbookrec()
  crtlibrec(bkd)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69418535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档