[Tutor] Custom Function that Takes argument

Alan Gauld alan.gauld at btinternet.com
Tue Sep 29 03:03:09 CEST 2015


On 29/09/15 00:45, Nym City via Tutor wrote:

> I am learning how to create custom functions and watched the
 > tutorial online that uses API for locu

Since most folks here probably don't know locu you should
maybe give us a URL. Then we can know what it is you are
talking about.

> I cannot figure out how to prompt a user to input their zip
 > code and use that information against the function.

The normal way is to pass it in as a parameter of the function:

def myfunc(aZipCode):
     print aZipCode

zip = raw_input('What is your zip: ')
myfunc(zip)


> import urllib2
> import json
>
> locu_api = 'not included here for obvious reasons'
> zip = raw_input('What is your zip: ')
>
> 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']
>
> print zip_search(zip_search())

Here you are passing the results of your own function
in as an argument to your function. That should give an
error since the inner call has no argument and the
function is looking for one.
You need to pass in your query string:

print zip_search(zip)

Always post any errors you get it helps us help you.


-- 
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