0

I am a complete beginner in python (2.7), and this program is supposed to take some students names and their marks in 3 subjects, and return the average of specified name. I did get the answer correctly(through another method), but this program isn't working properly and I want to know why. The main problem is that the list elements with odd indices aren't being deleted. The code is

num=int(raw_input("enter the numbeer of students:"))
d=dict()
marks=[]
for i in range(num):
    name=raw_input("enter student name")
    j=0
    try:
        while (j<3):
            del marks[j]
            j+=1
    except:
        print "okay!"
    print marks
    for i in range(3):
        marks.append(int(raw_input("enther makrs:")))
    print marks,"after"
    d[name]=tuple(marks)
req=raw_input("enter the name you want to check")
s=0
for key in d.keys():
    if req==key:
        n=d[key]
        l=list(n)
        ave=sum(l)/3
        print ave
    else:
        print "boo"

The output for the above program is:

vamshi@vamshi-HP-Notebook:~/python$ python u.py
enter the numbeer of students:2
enter student namev
okay!
[]
enther makrs:1
enther makrs:2
enther makrs:3
[1, 2, 3] after
enter student namek
okay!
[2] #why isn't this 2 deleted?
enther makrs:5
enther makrs:6
enther makrs:7
[2, 5, 6, 7] after
enter the name you want to check

Thanks in advance

1
  • Never, ever use a blank "except" statement, especially when it just prints "okay". As MisterMiyagi points out, this hides any problems your script encounters. Commented Jul 2, 2016 at 10:07

2 Answers 2

1

The loop is working properly, but your logic is flawed. Think about which elements you delete during iteration:

[1, 2, 3]
# delete 1st element, i.e. 1
[2, 3]
# delete 2nd element, i.e. 3!!!
[2]
# delete 3rd element, which doesn't exist

Part of your problem is also that your try: ... except: is masking the problem. It is inappropriate for what you want to do.

If you want to clear a list, you can just overwrite it with a new, empty one.

marks[:] = []
Sign up to request clarification or add additional context in comments.

3 Comments

One small doubt though. how to write this program without the use of try and except statement? I want a clean code. TIA
@varunaaruru since it looks like you are trying to delete the first three items, you can check if the len of marks is >= 3 else delete everything.
@varunaaruru The cleanest approach is to create a new list for every student. In general, reusing mutable data structures (lists, dicts, ...) is a bad idea, since you may get unwanted side effects. If you wouldn't make an explicit copy via tuple, cleaning the list for a student would remove the previous student's entries as well (since it's the same list).
0

The del operator deletes only the sepcified index but moves the left objects to the front. That means you are skipping parts of the list.

Try that code:

j=0
try:
    while (j<3):
        del marks[0]
        j+=1
except:
    print "okay!"

Another way to clean the list is:

del marks[:]

Which also makes very clear (good for documentation) what you are trying to achieve.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.