Skip to content

Is it in you?

October 27, 2009

I just had the most wonderful moment. At home after our second in-person meeting tonight, I was working my way through an exercise in Chapter 3. Continuing the coffee bean example program from Chapter 2, we were using the Twitter API to post coffee bean prices to Twitter (more on that later).

In summary, the exercise asked us to create two options for the user, an option to immediately report the current price of coffee beans or the option to monitor the price and send an alert when it dips below a certain point.  For immediate reporting, the example had it send the floating point number we had fetched as the price.  Well, I thought it would be a little odd to post a random number to my Twitter stream; I wanted to be able to surround it with text, including our group’s hash: “The price of coffee beans is now: $X.XX (Python rules!) #learnpython”

So I tried this:

if price_now == “Y”:
    send_to_twitter(“The price of coffee beans is now: $” + get_price() +         ” (Python rules!) #learnpython”)

But that resulted in a TypeError, as my definition of get_price() asked it to return a float.  Hmmm.  I needed to find a way to convert the price back into a string.  I had not yet been taught how to do that.  Okay, no worries.  I had seen str() somewhere before—I probably needed to use that.  How about :

if price_now == “Y”:
    send_to_twitter(“The price of coffee beans is now: $” + str(get_price())         + ” (Python rules!) #learnpython”)

No go.  Okay, let’s try:

if price_now == “Y”:
    return str(get_price())
    send_to_twitter(“The price of coffee beans is now: $” + get_price() +         ” (Python rules!) #learnpython”)

No good either.  I tried one or two other things, before arriving at this, which worked:

if price_now == “Y”:
    current_price = str(get_price())
    send_to_twitter(“The price of coffee beans is now: $” + current_price +         ” (Python rules!) #learnpython”)

When no error came back, I triumphantly ran over to my browser and refreshed my Twitter page, cheering aloud when my nice little post appeared!  (Then I revised my code because I had made a punctuation typo in my string, and we can’t have that now, can we?)

But none of that is the point.

The point is: it’s in me.  I wasn’t sure that is was, and now I know—it is.

And what, exactly, is “it”?  It is the bug.  It is the combination of native curiosity and stubbornness that made me play around with the code and take some wild guesses instead of running straight to Google (or choosing to stay within the bounds of the exercise).  That might sound like a small thing, but I know it is not.  I was determined to make the program do what I wanted it to do, I came up with a few guesses as to how to do that, and I kept trying different things until I succeeded (and then I felt thrilled).

As much as I have to learn, I know now that I really am hooked.  And that I’ll get there.

About these ads
28 Comments leave one →
  1. Yasmine permalink
    October 27, 2009 3:02 am

    Haha, that’s awesome. It definitely feels good getting this stuff to work. Good for you!

    What happened when you sent your first message to Twitter? Did it display right away and/or did anything appear when you ran it? I’m wondering because nothing is coming thru for me. At least not yet. Hopefully, ten “I’m sending this message to Twitter using Python” tweets don’t appear all of a sudden.

    The only other thing I can think of is that it’s not working because I have a protected profile. I turned it off but something to test out and include as feedback in the book just in case.

    -Yasmine

  2. October 27, 2009 7:28 am

    What a great post! You’ve just made my day. Cheers, Paul.

  3. October 27, 2009 12:30 pm

    I love this series of posts. Watching the evolution of your apps is fun!

    What error did you get when you used str() inline, in the second example? I’m probably missing something, but it looks like it should work to me.

    • October 27, 2009 1:26 pm

      I got another TypeError. I’m using Python 3.1.1; I don’t know if that would explain it, but I understand a lot has changed.

      • October 27, 2009 1:53 pm

        I wonder if it’s a unicode thing. I’ll have to experiment a little to see if I can reproduce it.

      • October 27, 2009 8:47 pm

        OK, I installed 3.1.1 and tried:

        'foo' + str(3.5) + 'bar'
        

        and got the string I expected as a result. I’d have to see your whole script to debug any further. Have you posted it online somewhere?

    • October 28, 2009 12:51 pm

      Okay, so I tried the second variation again and (as you can see in my Twitter stream to the right) it worked. Looking back at the error message I got the other night, I think it was an exact repeat of the first TypeError, leading me to think I somehow didn’t save the changes before rerunning the program. I’ll chalk that up to it being after midnight at the time. :-)
      Good to know that works!

      • October 28, 2009 12:52 pm

        Yay, I’m not nuts!

      • Ben Anhalt permalink
        October 29, 2009 9:03 pm

        In the second example, the addition operator between the strings str(get_price()) and '"(Python rules!) #learnpython"' occurs at the beginning of the third line, whereas in the working example it occurs at the end of the preceding line . Python2.6 does not seem to care, but Python3.x might be different. I don’t have it installed to check.

      • October 30, 2009 5:06 am

        The placement of the operator makes no difference … as the opening “(” and closing “)” associated with the print function delimit what gets printed. The placement of the “+” operator is of no consequence as it appears *inside* the parens. I’ve tried similar code in Python 3 and 2.6 – neither care where the “+” operator appears in this sort of code. –Paul.

  4. Bill Dietz permalink
    October 27, 2009 2:48 pm

    Way to go Julie!! Next thing you know, you will want to learn to fly. :-)

  5. David Griffiths permalink
    October 27, 2009 5:38 pm

    Hello Julie,

    The thing I love in these posts is that you are changing the code, bending it to your will and making it your own. I agree with Doug, I can’t see why one of the earlier versions didn’t work. The great thing was to see you chipping away until it did. Seeing that makes the work of the last year worthwhile.

    DG

    • October 28, 2009 5:17 am

      I echo David’s comments … we are thrilled to see you applying what you’ve learned from the book (and elsewhere). That’s what we were after! –Paul.

  6. October 27, 2009 5:57 pm

    It is nice to see how you managed to arrive at your answer. I used to learn to program in Python some years ago, but gave it up as the syntax errors and Python gotchas were just to much to deal with.

    I will definitely bookmark your blog :)

  7. October 27, 2009 7:55 pm

    I’ll continue to watch your progress. As the book’s editor, I’ve already followed along, but I didn’t think to change anything. It’s fun to see your attempts to add to or break the code.!

  8. Alex Esplin permalink
    October 28, 2009 11:10 am

    This would have been a perfect place to learn about format strings. Coming from a C background to python, format strings have been my salvation.

    In your case, a format string would have looked like:

    send_to_twitter(“The price of coffee beans is now: $%0.2f (Python rules!) #learnpython” % get_price())

    docs for format strings can be found here

    • October 28, 2009 2:02 pm

      Thanks, Alex, but… it’s important to master walking before you start running. Your comment on format strings is valid… and we cover them later in the book (when they are really needed to solve a problem) .Great conversations going on here. Keep it up! –Paul

  9. Fernando H. Sanches permalink
    October 28, 2009 11:52 am

    Nice post.

    It’s very nice to try and achieve what you want to do , perseverance is a gift in our world.

    However, it is important to understand why the changes you solved the problem (or not). Specially in the second code example, it’s important to know why it doesn’t work (it appears to be equivalent to the one which worked).

    In programming, many times we are temped to make “random” changes in the program to see if we can make things work. As long as you are understanding what these changes do, that’s fine. If you don’t, be careful – you could be developing a bad habit.

    Good luck in your learning!

    • October 28, 2009 12:02 pm

      Yeah, I do think this is a fair point. And I definitely intend to look into this more and figure out the underlying mechanisms, standards, and principles. (I’m quite intrigued that the second thing I tried should have worked, and as soon as I have a spare minute I will play around with it some more and try to figure out why I got an error.)

      As an English major and editor, I certainly have a healthy respect for attention to detail and for elegant, concise communication (whether with other human beings or with a computer). I also appreciate that breaking the rules from time to time is necessary and even sometimes more effective (depending on one’s goal) than following them, but one has to *know* the rules before one is entitled to break them. And I certainly recognize that I have a lot of learning to do before I can say that I know the rules of programming.

      But as I said in my post (and you echo at the top of your comment), my main concern and celebration here was discovering a genuine interest for the subject. Making it do what I wanted it to do was *fun*. Now that I know I feel that way, the rigor required to form good habits will be much more tolerable. :-)

  10. October 28, 2009 1:14 pm

    Flying is easy with Python, just import antigravity: http://xkcd.com/353/

    I’m loving these posts. Keep up the great journey, and have fun!

  11. October 29, 2009 7:34 am

    Good on you, Julie!

    I started learning programming 40 yearr+ ago as a trainee with big automotive company, doing a 6 months full-time in-house training program.

    I still enjoy it. :)

    If I can help in anyway. i am willing to.

    Great blog…

  12. October 29, 2009 11:50 am

    Nice one Julie. You’ve got the bug (pun intended) and once you’ve got it, you’ll always have it. Better stock up on your choice of caffeine supply and prepare yourself for lots of tears, heartache and grief. But, it’s all worth it. You will enjoy moments of pure exhilaration as your code runs without error and does exactly the task that you intended of it – makes you jump for joy.

    I think you’ll end up burning a lot of midnight oil.

  13. pete permalink
    November 1, 2009 8:43 pm

    what’s wrong with running to google to find out how something is done? you’re expected to know everything

    • November 1, 2009 8:55 pm

      There’s nothing wrong with Googling for help (especially when one is in a hurry). My point was just that I took satisfaction in figuring it out for myself, and in the process of trying to find a logical solution, which I might argue taught me more than just asking Google to supply the answer. Discovering within myself the willingness to wrestle with the code this way (instead of the urge to run for a “quick fix”) was important.

  14. November 20, 2009 6:48 pm

    Keep it up, Julie. You’re rockin’ it!

    I’m curious about when you say “The point is: it’s in me. I wasn’t sure that is was, and now I know—it is. And what, exactly, is “it”? It is the bug.”

    It’s a sort of Women in Technology question. Are dads like me, with 6 daughters, not doing enough to encourage their daughters to experiment with technology? Are we somehow throwing up unconscious impediments that make it oh so much easier to do something that’s traditionally “girlie”?

    Any thoughts on that? Best regards,

    -Kev

Trackbacks

  1. A word about women « Julie Learns Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: