简体   繁体   English

Python调用数据

[英]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. 我正在尝试编写一个程序,该程序将允许我输入学生姓名,然后通过输入学生ID编号进行搜索。 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! 感谢您提供的所有帮助!

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 student[idnum]替换为[idnum] ,这是您说出要查看的词典的方式
  2. Following python EAFP you try to find the key instead of asking if it's there first 在python EAFP之后,您尝试查找密钥,而不是先询问是否存在

Try changing [idnum] to idnum 尝试将[idnum]更改为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"

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

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