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
他们在命名上犯了很多错误
在添加学生
在搜索学生
在删除学生
在上面的更改之后,我运行它的运行,您还必须更改所有方法的变量的命名
输出
searchStudent():如果在函数中根本不使用
itMarks
参数,则不需要它。names
指的是名称列表,但您确实在尝试搜索name
。i
是试图与字符串串联的整数。不允许。它应该是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
子句来捕获这些错误。你知道吗祝你好运!你知道吗
我假设这是正确的形式:
注意我把名字改成了名字。 同样的错误再次出现
相关问题 更多 >
编程相关推荐