Is it in you?
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.