自定义python函数未运行

2024-09-29 23:31:08 发布

您现在位置:Python中文网/ 问答频道 /正文

names=["aaa","bbb","ccc","ddd","eee"]
itMarks=[90,98,87,98,78]

def printMainMenu():
    print(" Main Menu")
    print(" =========")

    print(" (1)Add Student")
    print(" (2)Search Student")
    print(" (3)Delete Student")
    print(" (4)List Student")
    print(" (5)Exit")

    choice = int(input("Enter Your choice[1-5]:"))
    return choice

def searchStudent(names,itMarks):
    name = input("Enter Name")
    i = names.index(names)
    print("Index is" + i)


def deleteStudent(student,itMarks):
    name = input("Enter Name to remove")
    student.remove(names)
    print("Successfully Deleted" + names)

def removeStudent(names):
    name = input("Enter name to remove")
    name.remove(name)
    print("Successfully deleted" + names)


def addStudent(names, itMarkas):
    name = input("Enter Name")
    names.append(names)
    itMarks = input("Enter IT Marks")
    itMarks.append(itMarks)

def listStudent(names, itMarks):
    for i in range(0, len(names)):
        print(names[1], "", itMarks[i])

names = []
itMarks = []

choice = 1
while choice >= 1 and choice <= 4:
    choice = printMainMenu()

    if choice == 1:
        addStudent(names, itMarks)

    elif choice == 2:
        searchStudent(names, itMarks)

    elif choice == 3:
        deleteStudent(names, itMarks)

    elif choice == 4:
        listStudent(names, itMarks)

    elif choice == 5:
        print("Exit from the program")
    else:
        print("invalid choice!")
        choice = 1

我对Python编程是新手。下面的Python代码是用来对数组执行一些任务的。有两个名为names和itMarks的数组。还有一些功能:

addStudent() - To add students to the array
searchStudent() - To search a student with in the list.
deleteStudent() - To delete the given student from the list.
listStudent() - To list out the all the names of the students in the list.

当程序运行时,它要求选择一个选项。然后根据他们的选择来完成任务。但是当我运行这个代码时,它会显示错误。 请帮帮我。提前谢谢。你知道吗

ERROR :

当我选择选项1(addstudent)并在错误为yield后输入name时。你知道吗

Traceback (most recent call last):
  File "C:\Users\BAALANPC\Desktop\new 3.py", line 59, in <module>
    addStudent(names, itMarks)
  File "C:\Users\BAALANPC\Desktop\new 3.py", line 42, in addStudent
    name = input("Enter Name")
  File "<string>", line 1, in <module>
NameError: name 'rtrt' is not defined

Tags: thenameininputnamesdefstudentremove
3条回答

他们在命名上犯了很多错误

添加学生

def addStudent(names, itMarkas):
    name = input("Enter Name")
    names.append(name)          # names cant appent it should be name
    itMark = input("Enter IT Marks")   # here itmark not itMarks
    itMarks.append(itMark)

搜索学生

 def searchStudent(names,itMarks):
     name = input("Enter Name")
     i = names.index(name)   # try to find index of name not names
     print("Index is" + i)

删除学生

def deleteStudent(student,itMarks):
    name = input("Enter Name to remove")
    student.remove(name)        # try to remove name not names
    print("Successfully Deleted" + name)

在上面的更改之后,我运行它的运行,您还必须更改所有方法的变量的命名

输出

 Main Menu
 =========
 (1)Add Student
 (2)Search Student
 (3)Delete Student
 (4)List Student
 (5)Exit
 Enter Your choice[1-5]:1
 add student
 Enter Name"aaa" 
 Enter IT Marks111
 Main Menu
 =========
(1)Add Student
(2)Search Student
(3)Delete Student
(4)List Student
(5)Exit
Enter Your choice[1-5]:
修改你的代码

searchStudent():如果在函数中根本不使用itMarks参数,则不需要它。names指的是名称列表,但您确实在尝试搜索namei是试图与字符串串联的整数。不允许。它应该是str(i)。你知道吗

deleteStudion():最好保持参数一致,使用names而不是student。同样,与上面相同的问题应该是.remove(name),您不需要itMarks参数。print语句应该引用name,而不是names。你知道吗

removeStudent():这是与deleteStudent()相同的代码,但没有使用,所以不确定它为什么在那里。你知道吗

addStudent():参数中输入错误,.append(name)。您有一个全局变量和一个局部变量名为同一事物,它们与程序冲突。将input设置更改为itMark.append(itMark)。你知道吗

listStudent()print语句有一个输入错误,1应该是i。不确定为什么也包含空字符串。你知道吗

在函数def的下面,将变量重列为空列表。当您试图在空列表中查找或修改某些内容时,这可能会导致许多函数出现ValueError。只需删除此代码。你知道吗

此外,任何错误都将break您的while循环。我建议添加更多的布尔或使用try except子句来捕获这些错误。你知道吗

祝你好运!你知道吗

我假设这是正确的形式:

def searchStudent(names,itMarks):
    name = input("Enter Name")
    i = names.index(name)
    print("Index is" + i)

注意我把名字改成了名字。 同样的错误再次出现

def deleteStudent(student,itMarks):
    name = input("Enter Name to remove")
    student.remove(name)
    print("Successfully Deleted" + names)

相关问题 更多 >

    热门问题