Re: Indexing Error

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: Indexing Error

sabareesh kumar
Hi Ankur
You are getting the error because you have initialized 'std' as an empty
list: std = []
and then indexing it: std[i]. If you are not sure of the size of the list
at the time of declaration, you will have to use the list method "append()"
to add elements to the list: std.append(STUDENT(roll,name,present,absent))


> On Sun, 19 Aug 2018, ankur gupta wrote:
>
> > Dear Sayantan, Mohit
> > Tried executing by implementing following change
> >
> > class STUDENT:
> > no_of_students=0
> > def __init__(self,roll,name,present,absent):
> > self.roll=roll
> > self.name=name
> > self.present=present
> > self.absent=absent
> > STUDENT.no_of_students+=1
> >
> > std= []
> > n='Y'
> > i=0
> > while n=='Y' or n=='y':
> > roll=input("ENTER NAME: ")
> > name=input("ENTER ROLL NO:")
> > present=input("ENTER NO. OF DAYS PRESENT:")
> > absent=input("ENTER NO. OF DAYS ABSENT:")
> > std[i] = STUDENT(roll,name,present,absent)
> > n=input("ENTER MORE RECORDS (Y/y)?...")
> > i+1
> >
> >
> > Received following error code?
> >
> > ankur@ankur-Lenovo-G50-80:~/PycharmProjects$ cd
> /home/ankur/PycharmProjects ; env "PYTHONIOENCODING=UTF-8"
> > "PYTHONUNBUFFERED=1" /home/ankur/anaconda3/bin/python
> >
> /home/ankur/.vscode/extensions/ms-python.python-2018.4.0/pythonFiles/PythonTools/visualstudio_py_launcher.py
> > /home/ankur/PycharmProjects 39355 34806ad9-833a-4524-8cd6-18ca4aa74f14
> RedirectOutput,RedirectOutput
> > "/home/ankur/PycharmProjects/STUDENTS ATTENDANCE.PY"
> > ENTER NAME: ANKUR
> > ENTER ROLL NO:1ENTER NO. OF DAYS PRESENT:12ENTER NO. OF DAYS
> ABSENT:8Traceback (most recent call last):
> > ? File "/home/ankur/PycharmProjects/STUDENTS ATTENDANCE.PY", line 18, in
> <module>
> > ??? std[i] = STUDENT(roll,name,present,absent)
> > IndexError: list assignment index out of range
> >
> > Pastebin page has been removed therefor attaching file along the mail
> >
> >
> > Thanks
> > Ankur Gupta
> >
> > On Sun, Aug 19, 2018 at 3:59 PM Sayantan <[hidden email]> wrote:
> >       Hello Ankur,
> >
> >       A few things before we take the program in context:
> >
> >       1. Always paste the code in a page(pastebin/paste.ubuntu.com &
> the like)
> >       so that the indentation remains proper. This will help the members
> to
> >       easily figure out the issue with the code.
> >
> >       2. Always refer to the error message provided by the interpreter
> before
> >       you approach an audience. You, may have in this case, already done
> that.
> >       This is just a suggestion.
> >
> >       Now, back to the code, the error is because there is no
> >       definition/information about 'std' in the code. The interpreter
> doesn't
> >       know in this case the type of 'std' itself. As a result, when you
> are
> >       trying to push a value into the std[i] variable/memory(a
> collection is
> >       what you have in mind), the interpreter is spewing this error
> message.
> >
> >       Defining std = [] or the specific collection type(dict/tuple) would
> >       resolve this error.
> >
> >       --
> >       Sayantan Bhattacharya
> >       [Sent from pine@dev-machine]
> >
> >       On Sun, 19 Aug 2018, ankur gupta wrote:
> >
> >       > ---------- Forwarded message ---------
> >       > From: ankur gupta <[hidden email]>
> >       > Date: Sun, Aug 19, 2018 at 3:07 PM
> >       > Subject: Please help with this code
> >       > To: <[hidden email]>
> >       >
> >       >
> >       > Dear Sir
> >       > I am new to? python programing, I am getting error while
> executing this
> >       > code. I want to generate multiple instance using while loop to
> store data
> >       > following is the code
> >       >
> >       > class STUDENT:
> >       > no_of_students=0
> >       > def __init__(self,roll,name,present,absent):
> >       > self.roll=roll
> >       > self.name=name
> >       > self.present=present
> >       > self.absent=absent
> >       > STUDENT.no_of_students+=1
> >       >
> >       > n='Y'
> >       > i=0
> >       > while n=='Y' or n=='y':
> >       > roll=input("ENTER NAME: ")
> >       > name=input("ENTER ROLL NO:")
> >       > present=input("ENTER NO. OF DAYS PRESENT:")
> >       > absent=input("ENTER NO. OF DAYS ABSENT:")
> >       > std[i] = STUDENT(roll,name,present,absent)
> >       > n=input("ENTER MORE RECORDS (Y/y)?...")
> >       > i+1
> >       >
> >       >
> >       > GETTING THIS ERROR
> >       >
> >       > ankur@ankur-Lenovo-G50-80:~/PycharmProjects$ cd
> /home/ankur/PycharmProjects
> >       > ; env "PYTHONIOENCODING=UTF-8" "PYTHO
> >       > NUNBUFFERED=1" /home/ankur/anaconda3/bin/python
> >       > /home/ankur/.vscode/extensions/ms-python.python-2018.4.0/pythonFi
> >       > les/PythonTools/visualstudio_py_launcher.py
> /home/ankur/PycharmProjects
> >       > 42323 34806ad9-833a-4524-8cd6-18ca4aa74f1
> >       > 4 RedirectOutput,RedirectOutput
> "/home/ankur/PycharmProjects/STUDENTS
> >       > ATTENDANCE.PY"
> >       > ENTER NAME: ANKUR
> >       > ENTER ROLL NO:1
> >       > ENTER NO. OF DAYS PRESENT:12
> >       > ENTER NO. OF DAYS ABSENT:8
> >       > Traceback (most recent call last):
> >       >? File "/home/ankur/PycharmProjects/STUDENTS ATTENDANCE.PY", line
> 17, in
> >       > <module>
> >       >? ? std[i] = STUDENT(roll,name,present,absent)
> >       > NameError: name 'std' is not defined
> >       >
> >       >
> >       > THANKS? IN ADVANCE
> >       > ANKUR GUPTA
>
_______________________________________________
BangPypers mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/bangpypers