【问题标题】:TypeError: print() takes 0 positional arguments but 1 was given ( highschool edition )TypeError: print() 接受 0 个位置参数,但给出了 1 个(高中版)
【发布时间】: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


【解决方案1】:

这可能对你有帮助,

创建一个带有名称和卷号的二进制文件。搜索给定的卷号并显示名称,如果找不到则显示相应的消息

import pickle

file = open("student_records.dat",'wb')


#Creating the Records

num = int(input("How many record do you want to fill : "))
for i in range(1,num+1):
    print("Enter the detail of student %d"%i)
    try:
        roll_no = int(input("Enter the roll number : "))
        name =  input("Enter the name : ")
        pickle.dump([roll_no,name],file)
    except :
        print("Something went wrong!") 
        pass
file.close()
        
    
#Display the name using roll number 
file = open("student_records.dat",'rb')
print()
print("Now the time to display the name using roll number ")
rol = int(input("Enter the roll number : "))
flag= False
while not flag:
    try:
        fetch = pickle.load(file)
        if fetch[0]==rol:
            print("Name of roll no ",rol," is : ",fetch[1])
            flag= True
            break
    except:
        print("Something went wrong!")
        break
if not flag:
    print("There is no such student with ",rol," roll number ") 

【讨论】:

    猜你喜欢
    • 2020-06-13
    • 2013-09-23
    • 2017-10-05
    • 2016-01-30
    • 2019-06-26
    • 2014-11-12
    • 1970-01-01
    • 2019-04-15
    相关资源
    最近更新 更多