【发布时间】:2021-08-26 12:59:41
【问题描述】:
我收到了一些未知的多重错误。
###Program 11 Create a binary file with name and roll no. Search for a given roll number and display the name, if not found display appropriate message.
import pickle
import sys
D={}
def main(self):
try:
no=input("enter the number of students")
file=open("studentsdetails.dat", "wb+")
for i in range (no):
print("enter the information of student %d"%i)
D["rollno"]=int(input("enter the roll number: "))
D["Sname"]=input("enter the student name: ")
D["marks"]=int(input("enter the marks: "))
pickle.dump(D,file)
file.close()
except:
input("there was some error,press any key to continue")
pass
def print():
try:
file=open("studentsdetails.dat", "rb")
while True:
out=pickle.load(file)
print(out)
except:
print("error try again")
pass
def search():
try:
file=open("studentsdetails.dat","rb")
se=input("enter the roll number to search")
while True:
if D["rollno"]==se:
print("the rollnumber %d the data is:"%se)
print(D)
else:
print("no record found")
break
file.close()
except:
print("some error occured, try again")
pass
while True:
intg=input("Enter your choice:\n1. Enter data\n2. Print Data\n3.search data\n4. Exit\n")
if intg==("1"):
main()
if intg==("2"):
print()
if intg==("3"):
search()
if intg==("4"):
break
我已经在testuploader.com 上传了鳕鱼,这里是:http://txt.do/18as3
【问题讨论】:
-
将您的
print函数重命名为其他名称。 -
def print():将内置 print 函数中的“print”重新绑定到您自己的不带参数的函数。小心不要用你自己的函数遮蔽任何 buitlin。 -
谢谢,我没注意到,不好意思问了这么含糊的东西
-
如果您在上面代码中的搜索功能中帮助我,这将是一个很大的帮助,它会给出这个错误Traceback(最近一次调用最后一次):文件“C:\用户\Nitin\Desktop\nit. py”,第 54 行,在
search() 文件“C:\Users\Nitin\Desktop\nit.py”,第 37 行,在搜索 if D["rollno"]==se: KeyError: 'rollno'
标签: python python-3.x typeerror