1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

First Python Problem

Discussion in 'Scripting & Programming' started by tripwire45, Nov 2, 2007.

  1. tripwire45
    Honorary Member

    tripwire45 Zettabyte Poster

    13,493
    179
    287
    I knew I'd be asking a lot of questions as I started my wanderings into a completely new discipline (for me), but I didn't think it would be at such an elementary level as lists and dictionaries. Here's what the examples are in my source material (suitably altered to avoid violating copyright laws)
    Code:
    >>> addressBook = [
    ... ['Wilma', '9 Rock St',' Bedrock', '56789'],
    ... ['Betty', '11 Rock St', 'Bedrock', '56789']
    ... ]
    >>>
    
    >>> addressBook = {
    ... 'Wilma' : ['Wilma', '9 Rock St',' Bedrock', '56789'],
    ... 'Betty' : ['Betty', '11 Rock St', 'Bedrock', '56789']
    ... }
    >>>
    Now the line breaks are inserted by the author because of the practicalities of teaching on a website. I'm just typing everything "in-line". Here are my results:
    Code:
    Python 2.4.3 (#2, Oct  6 2006, 07:52:30)
    [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> addressBook = [['Wilma', '9 Rock St','Bedrock', '56789'],['Betty', '11 Rock St','Bedrock', '56789]]
      File "<stdin>", line 1
        addressBook = [['Wilma', '9 Rock St','Bedrock', '56789'],['Betty', '11 Rock St','Bedrock', '56789]]
                           ^
    SyntaxError: EOL while scanning single-quoted string
    >>> addressBook = {'Wilma' : ['Wilma, '9 Rock St','Bedrock', '56789'],['Betty' : ['Betty', '11 Rock St', 'Bedrock', '56789']}
      File "<stdin>", line 1
        addressBook = {'Wilma' : ['Wilma, '9 Rock St','Bedrock', '56789'],['Betty' : ['Betty', '11 Rock St', 'Bedrock', '56789']}
                                           ^
    SyntaxError: invalid syntax
    >>>
    
    The "line breaks" (if you want to call them that) in my code are wrap arounds in the bash shell on my Linux box. It seems like this should be so simple. Where am I introducing the syntax error (yeah...line 1, but where)?
     
    Certifications: A+ and Network+
  2. tripwire45
    Honorary Member

    tripwire45 Zettabyte Poster

    13,493
    179
    287
    Oh duh. I got it to work. I forgot how much indenting mattered to Python. The "dots" (...) shown in this example:
    Code:
    >>> addressBook = {
    ... 'Wilma' : ['Wilma', '9 Rock St',' Bedrock', '56789'],
    ... 'Betty' : ['Betty', '11 Rock St', 'Bedrock', '56789']
    ... }
    >>>
    These are meant to represent indents which can be introduced by hitting the tab key. Now if someone can help me remember how to introduce a line break in my code without hitting Enter? :oops:

    Here's how I have it now:
    Code:
    >>> addressBook = {     'Fred' : ['Fred', 'Address'],   'Rose' : ['Rose', 'Address']    }
    >>> print addressBook['Rose']
    ['Rose', 'Address']
    >>>
    
     
    Certifications: A+ and Network+
  3. ffreeloader

    ffreeloader Terabyte Poster

    3,661
    106
    167
    Trip,

    A dictionary is a listing of pairs:

    >>> Dictionary = {'name' : 'Wilma', 'address': '9 Rock St', 'city': 'Bedrock', 'zipcode' : '56789'}
    >>> Dictionary['name']
    'Wilma'
    >>>

    That should be key:value pairs.

    >>> Dictionary.keys()
    ['city', 'zipcode', 'name', 'address']
     
    Certifications: MCSE, MCDBA, CCNA, A+
    WIP: LPIC 1
  4. tripwire45
    Honorary Member

    tripwire45 Zettabyte Poster

    13,493
    179
    287
    Thanks, Freddy. I got it figured out. Thanks for not laughing at me for being such a newbie. :oops:
     
    Certifications: A+ and Network+
  5. ffreeloader

    ffreeloader Terabyte Poster

    3,661
    106
    167
    Trip,

    Why would I laugh at you?

    If you see me laughing "at" somebody, it's usually because in my head/heart I'm laughing with them, because "I've been there and done that" myself, and its a relief to see I'm not the only person who made that same mistake. I may not always actually put that in writing, but it's not very often I would laugh at someone because they didn't know something. I'm ignorant in so many areas of knowledge, and have such a superficial knowledge in so many other areas, that there is far more that I don't understand, than I do understand.

    Will I get sarcastic at times if I'm asked, or told, something that is completely self-evident, or should be to almost everyone who at least thinks about what has been said? Yes. But, that's mostly because I feel that what they said was sarcastic towards me, and I'm just returning the barb with compliments. Something like, sarcasm is its own just reward.....
     
    Certifications: MCSE, MCDBA, CCNA, A+
    WIP: LPIC 1

Share This Page

Loading...