This course will become read-only in the near future. Tell us at if that is a problem.

The Force is Strong With This One

Turn your search term into a user-specified variable

Your script is running smoothly and looking lovely! Congratulations. 

There's one small modification we could make to the script so that it could be useful for any search term. That's a lot of search terms :) And it certainly makes the script a lot more flexible. 

You have two options for implementing this functionality. Command line arguments and user prompt. Pick one!

Command Line Arguments

The first is to make your program take an argument when you first run it from the command line. Instead of

$ ruby myscript.rb

you would run

$ ruby myscript.rb searchterm

(and similarly for python). 

When you pass arguments to a script in this way, they are stored in a list named, in both languages, ARGV (for historical reasons). In Ruby, ARGV is a global array, and the first argument to your script is stored in ARGV[0].

In Python, in order to access the ARGV variable you must import the sys module (import sys). The first program argument is stored in sys.ARGV[1].  

User Prompt

A user prompt is a line of code that prompts the user for some kind of input. The program will wait until the user hits enter before continuing. Both Ruby and Python have a simple way of doing this. 

Of course, you want some descriptive text to accommodate the prompt, otherwise the user might not be clear what the program is waiting for. If needed, insert an appropriate print statement before the prompt to explain what you would like from the user.

For python, read about the raw_input function.  For ruby, you'll want to look into the gets function. See for example this reference. 

Make note of how the input is stored, and remember the variable name that you chose so you can use it as described below.

Tweaking your API Call

Now that you have your search_term variable, you'll need to modify the api_call string (remember, from task 5?) so that the search term is inserted into the url. You can do this either by using string formatting, as we did in the previous task, or you can use string concatenation.

Read about string concatenation or formatting if you need to, and then make the necessary change to your script. Once you have inserted the search_term variable into the api_call string, everything else is the same. Not so bad, is it?

Did you do any error checking to see if the user submitted a value before retrieving it? What happens if the user doesn't give an argument? Where does your program fail? How could you make it more robust? 

Post the approach you took to the comments, and share any particularly good resources you came across.  You don't have to implement error checking for this challenge, but it's good practice to start thinking about it! (And you are, of course, most welcome to implement it if you like). 

Task Discussion

  • Eenvincible said:

    Hi guys, I just completed this level of the challenge and it was so much fun.

    Please find my work on my blog where I post them regularly:

    Thank you and please let me know what you think!

    on Jan. 9, 2013, 11:31 p.m.
  • govindreddy said:


    import urllib2
    import json
    import sys
    def search_twitter(query='python'):
      url = '' + query
      response = urllib2.urlopen(url).read()
      data = json.loads(response)
      return data['results']

    def print_tweets(tweets):
      for tweet in tweets:
        print tweet['from_user'] + ': ' + tweet['text'] + '\n'

    if __name__ == "__main__":
      query=raw_input("Input your choice:")
      #query = sys.argv[1]
      results = search_twitter(query)

    on Dec. 7, 2012, 1 a.m.
  • manicphase said:

    Using the user prompt method, you can put your input request in a while loop that doesn't exit (therefore doesn't execute the API request) until specified conditions are met.

    on June 1, 2012, 8:20 p.m.