Reputation: 27
I am writing a program that allows the user to input student records, view records, delete records and display averages of marks. I am having trouble with removing a user inputted name from the list along with the students marks. This is the code i have so far
studentlist=[]
a=1
while a!=0:
print ("""
1. Add new student records
2. Show all student records
3. Delete a student record
4. Dislay overall average coursework mark
5. Display overall average exam mark
6. Calculate average marks
0. Exit
Plese select an option
""")
a=int(input(""))
if a==1:
name=input("Enter a students name: ")
cmark=int(input("Enter the students coursework mark: "))
emark=int(input("Enter the students exam mark: "))
student=(name,cmark,emark)
print (student)
studentlist.append(student)
student=()
if a==2:
for n in studentlist:
print ("Name:", n[0])
print ("Courswork mark:",n[1])
print ("Exam mark:", n[2])
print ("")
if a==3:
name=input("Enter a students name: ")
for n in studentlist:
if n[0]==name:
studentlist.remove(n[0])
studentlist.remove(n[1])
studentlist.remove(n[2])
Upvotes: 2
Views: 9534
Reputation: 45241
You can't delete members of a tuple
- you have to delete the entire tuple
. For example:
x = (1,2,3)
assert x[0] == 1 #yup, works.
x.remove(0) #AttributeError: 'tuple' object has no attribute 'remove'
tuple
s are immutable, which means they can't be changed. As the error above explains, tuple
s do not have a remove attribute/method (how could they? they are immutable).
Instead, try deleting the final three lines from your code sample above and replace them with the line below, which will simply remove the entire tuple
:
studentlist.remove(n)
If you want to be able to change or delete individual grades (or correct a student's name), I suggest storing the student information in a list
or a dict
instead (example using a dict
is below).
studentlist=[]
a=1
while a!=0:
print ("""
1. Add new student records
2. Show all student records
3. Delete a student record
4. Dislay overall average coursework mark
5. Display overall average exam mark
6. Calculate average marks
0. Exit
Plese select an option
""")
a=int(input(""))
if a==1:
promptdict1 = {'name': 'Enter a students name: ', \
'cmark': 'Enter the students coursework mark: ', \
'emark': 'Enter the students exam mark: '}
studentlist.append({'name': input(promptdict1['name']), \
'cmark': int(input(promptdict1['cmark'])), \
'emark': int(input(promptdict1['emark']))})
print(studentlist[-1])
if a==2:
promptdict2 = {'name': 'Name:', \
'cmark': 'Courswork mark:', \
'emark': 'Exam mark:'}
for student in studentlist:
print(promptdict2['name'], student['name'])
print(promptdict2['cmark'], student['cmark'])
print(promptdict2['emark'], student['emark'], '\n')
if a==3:
name=input("Enter a students name: ")
for n in studentlist:
if n['name']==name:
studentlist.remove(n)
Upvotes: 4
Reputation: 113978
it probably makes more sense to just overwrite the old list with a new list
studentList = [item for item in studentList if item[0] != name]
if you really want to remove it you should not modify a list while you iterate over it...
for i,student in enumerate(studentList):
if student[0] == name:
studentList.pop(i)
break #STOP ITERATING NOW THAT WE CHANGED THE LIST
Upvotes: 2