Jack
Jack

Reputation: 191

Python Recalling Data

I'm trying to write a program that will allow me to input student names and then search for them by typing in a student ID number. However, I keep hitting roadblocks and I'm not sure why. Here's my code so far.

students={}


def add_student():
  #Lastname, Firstname
  name=raw_input("Enter Student's Name")
  #ID Number
  idnum=raw_input("Enter Student's ID Number")
  #D.O.B.
  bday=raw_input("Enter Student's Date of Birth")
  print "Student Added!" 



  students[idnum]={'name':name, 'bday':bday}


def delete_student():
  idnum=raw_input("delete which student:")
  if idnum in students:
     del students[idnum]

def find_student():
  print "Find" 
idnum=raw_input("Enter Student ID:")
  if [idnum] in students:
    print "Name:"[idnum]["name"]
    print "ID Number:"[idnum]["idnum"]
    print "Date of Birth:"[idnum]["bday"]


def show_student_record():
  idnum=raw_input("show which student's records?:")
  if idnum in students:
    print "Name:"[idnum]["name"]
    print "ID Number:"[idnum]["idnum"]
    print "Date of Birth:"[idnum]["bday"]



menu = {}
menu['1']="Add Student." 
menu['2']="Delete Student."
menu['3']="Find Student"
menu['4']="Exit"
menu['5']="Show Student Record"

while True: 
  options=menu.keys()
  options.sort()
  for entry in options: 
     print entry, menu[entry]

selection=raw_input("Please Select:") 
if selection =='1': 
    add_student()
elif selection == '2': 
    delete_student()
elif selection == '3':
    find_student() 
elif selection == '5':
    show_student_record()
elif selection == '4': 
    break
else: 
    print "Unknown Option Selected!" 
with open('Students', 'w') as saveFile:
  saveFile.write("Records" + "\n")

name=[]
idnum=[]
bday=[]


with open('Students', 'r') as inFile:
  for line in inFile:
    line = line.rstrip()


name.append(line[0]) 
idnum.append(line[1]) 
bday.append(line[2])

Anytime I try enacting the find student function I get an error saying

Traceback (most recent call last):
  File "<stdin>", line 83, in <module>
  File "<stdin>", line 48, in find_student
  NameError: global name 'idnum' is not defined

Thanks for any and all help!

Upvotes: 0

Views: 307

Answers (2)

SkaeX
SkaeX

Reputation: 1

Try changing [idnum] to idnum

def find_student():
print "Find" 
idnum=raw_input("Enter Student ID:")
if idnum in students:
    print "Name:"[idnum]["name"]
    print "ID Number:"[idnum]["idnum"]
    print "Date of Birth:"[idnum]["bday"]
else:
    print "Student not found"

Upvotes: 0

Guy Gavriely
Guy Gavriely

Reputation: 11396

def find_student():
    print "Find" 
    idnum=raw_input("Enter Student ID:")
    try:
        print "Name: " + students[idnum]["name"]
        print "ID Number: " + idnum
        print "Date of Birth: " + students[idnum]["bday"]
    except KeyError, ex:
        print ex.message + ' not found'

note that:

  1. student[idnum] replaced [idnum], this is how you state what dictionary you want to look at
  2. Following python EAFP you try to find the key instead of asking if it's there first

Upvotes: 1

Related Questions