title: Python shows name error for a dictionary that exists
body: <p>For my school project, im making a library management system, in which the book ID assigned to a student should be there in the keys of another dictionary, bkd(which stores book details and the keys are the book IDs).
  The student records are in a file library.dat and the book records are in a file bookrec.dat
  The student records are in form of a dictionary(rec) and book details are in dictionary(bkd)
  Now, when i try to use an if statement to check whether the book id entered by a user is there in bkd.keys(), the program gives a name error. Any solutions?</p>
  <pre><code>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(&quot;Enter the Grade of the student: &quot;))
      div=input(&quot;Enter the Class of the student: &quot;)
      bookid=int(input(&quot;Enter the Book ID: &quot;))
      retdate=input(&quot;Enter the date of return: &quot;)
      if bookid in bkd.keys():
          rec[rno]={'Name':nm,'Grade':gd,'Division':div,'Book ID':bookid,'Return Date':retdate}
          p.dump(rec,f)
          print(&quot;Record Created Successfully..&quot;)
      else:
          print(&quot;Enter a valid Book ID..&quot;)
  </code></pre>
  <p>The dictionary bkd is created in another function crtbookrec()</p>
  <p>Here is crtbookrec:</p>
  <pre><code>def crt_book_rec():
      g=open('bookrec.dat','ab')
      bkd={}
      bkid=int(input('Enter Book ID: '))
      bknme=input('Enter Book Name: ')
      authnme=input(&quot;Enter Author Name: &quot;)
      bkd[bkid]={'Book Name':bknme,'Author Name':authnme}
      p.dump(bkd,g)
      g.close()
  </code></pre>
  <p>Error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\Users\kesha\Desktop\Project\Proj copy for testing\proj test.py&quot;, line 410, in &lt;module&gt;
      menufn()
    File &quot;C:\Users\kesha\Desktop\Project\Proj copy for testing\proj test.py&quot;, line 350, in menufn
      crtlibrec()
    File &quot;C:\Users\kesha\Desktop\Project\Proj copy for testing\proj test.py&quot;, line 19, in crtlibrec
      if bookid in bkd.keys():
  NameError: name 'bkd' is not defined
  </code></pre>
  <p>Menu that calls the functions:</p>
  <pre><code>def menufn():
      menu1='1.Student Details\n2.Book Details\n3.Exit'
      while True:
          print('='*90)
          print(&quot;\n\t\t\t    ABC Public School\n\n\t\t\t     Kottayam,Kerala\n\n\t\t\t    LIBRARY MANAGEMENT\n&quot;)
          print('='*90)
          print(menu1)
          print('='*90)
          opt1=input(&quot;Enter your choice(1/2/3): &quot;)
          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(&quot;\n\t\t\tSTUDENT DETAILS PROGRAM\n&quot;)
                  print(menu2,&quot;\n&quot;)
                  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==&quot;2&quot;:
              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(&quot;\n\t\t\tBOOK DETAILS PROGRAM\n&quot;)
                  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==&quot;3&quot;:
              print(&quot;Thank You for using the Library Management Program&quot;)
              break
          else:
              print(&quot;Invalid Choice..&quot;)
  menufn()
  </code></pre>
  <p>im sorry if the code is badly commented or written, im new to python</p>