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代码以使用该数组执行某些任务。有两个数组命名名称和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(添加学生)并输入错误后输入名称。
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
答案 0 :(得分:0)
我假设这是正确的形式:
def searchStudent(names,itMarks):
name = input("Enter Name")
i = names.index(name)
print("Index is" + i)
请注意我将名称更改为name。 也是同样的错误
def deleteStudent(student,itMarks):
name = input("Enter Name to remove")
student.remove(name)
print("Successfully Deleted" + names)
答案 1 :(得分:0)
他们在命名方面犯了太多错误
在 addStudent
中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)
在 searchStudent
中 def searchStudent(names,itMarks):
name = input("Enter Name")
i = names.index(name) # try to find index of name not names
print("Index is" + i)
在 deleteStudent
中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]:
答案 2 :(得分:0)
tl; dr修改你的代码
searchStudent():如果您根本没有在函数中使用itMarks
参数,则不需要names
参数。 name
指的是名称列表,但您确实在尝试搜索i
。 str(i)
是一个试图与字符串连接的整数。不允许。它应该是names
。
deleteStudent():最好保持参数一致,并使用student
而不是.remove(name)
。同样,与上面相同的问题应该是itMarks
,您不应该需要print
参数。 name
语句应引用names
而非.append(name)
。
removeStudent():这与 deleteStudent()的代码相同,但没有使用,所以不确定为什么会这样。
addStudent():参数中的拼写错误input
。你有一个全局变量和一个名为相同的本地变量,它们与程序冲突。将itMark
设置更改为.append(itMark)
和print
。
listStudent():1
语句有拼写错误,i
应为def
。不知道为什么还包括空字符串。
在您的函数ValueError
下方,您将变量重新显示为空列表。当您尝试查找某些内容或修改空列表中的某些内容时,这可能导致break
来自您的许多功能。只需删除此代码即可。
此外,任何错误都会while
您的try except
循环。我建议添加更多的布尔值或使用int3
子句来捕获这些错误。