Can't understand scope error.

This is the place for queries that don't fit in any of the other categories.

Can't understand scope error.

Postby Hypernova » Tue Jun 25, 2013 10:30 am

For some reason, when I run this code(an exercise for learning python on this site: http://cscircles.cemc.uwaterloo.ca/15a-basic/#pybox6 ), the error says that using [location] for the indices in the list: prog, cannot be done because [location] isn't an integer. But it is defined as an integer above: location = 0.

So is the while loop beyond the scope of the original value of location?

prog- is a list of strings.
The print statement is just for me trying to debug.
The if statement looks for a condition to end the loop.

Code: Select all
# here is a broken solution to get you started
def execute(prog):
   location = 0
   T=0
   while True:
      if location==len(prog)-1:
         return "success"
      prog[location].split()
      print(prog[location])
      location = findLine(prog, T)

Traceback (most recent call last):
  In line 8 of the code you submitted:
    prog[location].split()
TypeError: list indices must be integers, not NoneType





As a small aside, it looks as if the original definition of location is used as a way to refer to list index 0, but then the value of location is updated by location=findLine(prog,T). Which to me, seems to make the use of location as a counter pointless in the way it's used.

Any help or discussion about this will be great. Many thanks.
Hypernova
 
Posts: 18
Joined: Mon Jun 10, 2013 12:23 am

Re: Can't understand scope error.

Postby micseydel » Tue Jun 25, 2013 11:18 am

findLine() is return a non-integer value. I suggest you always post all the code, and don't try to summarize the traceback as you did. Tracebacks are great, and if you provide all the code we can give a very precise answer or explanation.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 929
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Can't understand scope error.

Postby Hypernova » Thu Jun 27, 2013 9:47 am

Hi micseydel, thanks for the tip. I realized what was happening thanks to you informing me of the real nature of the error. Sorry about the broken code, the code was broken up into separate exercises and I didn't think to add the separate pieces of code.
Hypernova
 
Posts: 18
Joined: Mon Jun 10, 2013 12:23 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], mckryall and 5 guests

cron