[Tutor] Custom Function that Takes argument

Nym City nymcity at yahoo.com
Tue Oct 13 01:32:26 CEST 2015


Thank you for your response. I updated the first portion of my code to include breaks: 

import urllib2
import json

locu_api = 'redacted'

ans=True
while ans:
    print ("""
    1.Search by City
    2.Search by Region (State Abbreviation)
    3.Search by Zip
    4.Exit/Quit
    """)
    ans=raw_input("What would you like to do? ")
    if ans=="1":
      locality = raw_input("\nEnter City ")
      break
    elif ans=="2":
        region = raw_input("\n Search by State ")
        break
    elif ans=="3":
        zip = raw_input("\n Search by Zip ")
        break
    elif ans=="4":
      print("\n Goodbye")
      break

def locu_search(query):
    api_key = locu_api
    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
    locality = query.replace(' ', '%20')
    final_url = url + '&locality=' + locality + "&category=restaurant"
    jason_obj = urllib2.urlopen(final_url)
    data = json.load(jason_obj)
    for item in data['objects']:
        print item['name'], item['phone'], item['street_address']


def region_search(query):
    api_key = locu_api
    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
    region = query.replace(' ', '%20')
    final_url = url + '&region=' + region + "&category=restaurant"
    jason_obj = urllib2.urlopen(final_url)
    data = json.load(jason_obj)
    for item in data['objects']:
        print item['name'], item['phone'], item['street_address'], item['locality'], item['website_url']

def zip_search(query):
    api_key = locu_api
    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
    zip = query.replace(' ', '%20')
    final_url = url + '&zip=' + zip + "&category=restaurant"
    jason_obj = urllib2.urlopen(final_url)
    data = json.load(jason_obj)
    for item in data['objects']:
        print item['name'], item['phone'], item['street_address'], item['categories'], item['website_url']

def non_empty_variable(varibles):
    for varible in varibles:
        if varible != "":
            print varible

varibles = [locu_search(locality), region_search(region), zip_search(zip)]


---------------------------------
With the above updates, the user prompts are working fine now. Thanks!

The issue that I am trying to solve now is print the final result.  The use case is that by not knowing which of the 4 options the user will select, i need a way to figure out which selection user made, run it against the appropriate query and than print out the result. 
My thought process is to narrow down the user selection by figuring out which of the 4 choices is not blank. To do this I created this:
def non_empty_variable(varibles):
    for varible in varibles:
        if varible != "":
            print varible

varibles = [locu_search(locality), region_search(region), zip_search(zip)]
______________________________I think the above should work but I get traceback error:
Traceback (most recent call last):
  File "C:/Users/dell/Documents/Python/MyProject/API_Projects/locu/locuAPI.py", line 70, in <module>
    varibles = [locu_search(locality), region_search(region), zip_search(zip)]
NameError: name 'locality' is not defined
--------The error points back to where this is:
varibles = [locu_search(locality), region_search(region), zip_search(zip)]
I don't understand why Python thinks that locality (and others) are not defined when I defined them at the very beginning. 


___________________________After I get this new issue resolved, I will go back and review urllib query functions. 
 Thank you. 


     On Sunday, October 11, 2015 6:25 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
   
 

 On 11/10/15 22:22, Nym City wrote:
> import urllib2
> import json
>
> locu_api = 'redacted'
>
> ans=True
> while ans:
>    print ("""
>    1.Search by City
>    2.Search by State
>    3.Search by Zip
>    4.Exit/Quit
>    """)
>    ans=raw_input("What would you like to do? ")
>    if ans=="1":
>      print("\n Enter City")
>    elif ans=="2":
>      print("\n Search by State")
>    elif ans=="3":
>      print("\n Search by Zip")
>    elif ans=="4":
>      print("\n Goodbye")
>    elif ans !="":
>      print("\n Not Valid Choice Try again")
>
>

Note that you never set ans to any false value so it will keep on looping
until you do (by entering an empty string).

You probably need to use break to exit the loop whenb you get valid input.

> # def locu_search(query):
> #    api_key = locu_api
> #    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
> #    locality = query.replace(' ', '%20')
> #    final_url = url + '&locality=' + locality + "&category=restaurant"

Note that you should not try to build the query string yourself, there are
functions in the standard library that will do most of the work and they
will be more reliable than anythong you can write yourself.

For example look at the urllib module (for 2.7, its the urlib.parse module
in v3) which contains several utility functions such as quote() and
urlencode()


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


 
  


More information about the Tutor mailing list