Why am I receiving 'list' object cannot be interpreted as an integer and 'str' object cannot be interpreted as an integer?


#1

Hi,
I wrote the following code and receiving error. What is problem with and how can I eradicate errors.

c=0
x=[]
y=[]
z=[]

while True:

    o=int(input("enter menu:"))
    if o==1:
        x.append(input("enter name:"))
        z.append(int(input("enter #:")))
        y.append(int(input("enter score:")))
        c+=1
      

    elif o==2:
        n=int(input("enter student number:"))
        for i in range(0,c):
            if n==z[i]:
                x.pop([i])
                y.pop([i])
                z.pop([i])
    elif o==3:
         for i in range(0,c):
             print("name:",x[i],"st number:",z[i],"score:",y[i])
    elif o==4:
        break

I received TypeError: 'list' object cannot be interpreted as an integer for x.pop([i])
then I changed that to x.pop(x[i]) and I received TypeError: 'str' object cannot be interpreted as an integer


#2

Did you try with just pop(i).?

Generally speaking, if we only wish to delete without returning the value,

del(x[i])

will do the same thing.


#3

Thanks for your valuable answer.I replace pop with del, but I get that error again.
The thing which is strange for me is that I am using the same definition for remove as same as find, but I only receive error from remove.

Meanwhile, there is a problem with finding maximum on o=2. Please take a look at my run below:

o=1:::add o=2:::max o=3:::find o=4:::remove o=5:::report
o=6:::exit enter menu:1

enter name:sam enter #:5 enter score:90

o=1:::add o=2:::max o=3:::find o=4:::remove o=5:::report
o=6:::exit enter menu:1

enter name:susan enter #:10 enter score:75

o=1:::add o=2:::max o=3:::find o=4:::remove o=5:::report
o=6:::exit enter menu:1

enter name:peter enter #:2 enter score:80

o=1:::add o=2:::max o=3:::find o=4:::remove o=5:::report
o=6:::exit enter menu:5

name: sam st number: 5 score: 90 name: susan st number: 10 score: 75 name: peter st number: 2 score: 80

o=1:::add o=2:::max o=3:::find o=4:::remove o=5:::report
o=6:::exit enter menu:2

max= 90

o=1:::add o=2:::max o=3:::find o=4:::remove o=5:::report
o=6:::exit enter menu:5

name: sam st number: 5 score: 90 name: susan st number: 10 score: 90 name: peter st number: 2 score: 90

o=1:::add o=2:::max o=3:::find o=4:::remove o=5:::report
o=6:::exit enter menu:6

You can see when I enter report as the first time, I will have true results. When I use max after that I find maximum number, although when I use again report you can see that I get false results(the score of sam, susan and peter are changed to 90 all of them) How can I eradicate this problem?


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.