#1
  1. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407

    Mind Refresher :D


    Sorry i kinda been really really busy and i kinda forgot somethings about python so alittle mind refreshment will help me alot

    Code:
    #!/usr/sbin/env python
    
    def shows(var1,var2):
        showdata[var1,var2]
    
        for c in range(len(showdata)):
            print c, showdata[c]
            
    shows("hi","ho")
    I get an error here & im tring to remember how to fix it

    Error---|
    Code:
    Traceback (most recent call last):
      File "1.py", line 9, in ?
        shows("hi","ho")
      File "1.py", line 4, in shows
        showdata[var1,var2]
    NameError: global name 'showdata' is not defined
    I kinda forgot python cause i got hired to do some php work for a long time.. but i CANT FORGET ABOUT python so now im back from start kinda still i can refresh my mem
    Last edited by xlordt; March 4th, 2004 at 01:24 PM.
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Welcome back,
    I can tell you've spent too much time on other langauges!
    I guess you are trying to create a local variable and assign a list to it.
    showdata = [var1,var2]
    will do the trick

    Because of that little buglet the local variable has not been created so the compiler looks in the global scope to find out what showdata might be and can't find it. I suspect it does not even get as far as trying to figure out what
    showdata[var1,var2] would compute to

    Here is one possibility:
    Code:
    >>> a = {}
    >>> a[1,2]="test"
    >>> a
    {(1, 2): 'test'}
    Where a tuple is the key!

    Grim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Code:
    for c in range(len(showdata)):
            print c, showdata[c]
    Also, if you're using Python 2.3, the New Way(tm) of tidying up that range(len(x)) code is the 'enumerate' method, like so:

    Code:
    for index, value in enumerate(showdata):
        print index, value
  6. #4
  7. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    nice thanx... & ya php/c got my head blowded lol thanx again
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    11
    It might be easier (read: better) to write:
    Code:
    for index, value in enumerate(showdata):
        print index, value
    This will only work if you're using Python 2.3.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hi perc, isn't that... exactly what sfb just said . Look up

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    11
    Originally Posted by netytan
    Hi perc, isn't that... exactly what sfb just said . Look up

    Mark.
    *sigh*
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    Originally Posted by percivall
    It might be easier (read: better) to write:
    Code:
    for index, value in enumerate(showdata):
        print index, value
    This will only work if you're using Python 2.3.
    OMG I never knew you could do that! I've been using whiles! Well I guess you learn something new every day

IMN logo majestic logo threadwatch logo seochat tools logo