[Tutor] Custom Function that Takes argument

Nym City nymcity at yahoo.com
Sun Oct 11 23:22:06 CEST 2015


Thank you for your feedback. I modified the code to reflect your suggestion and it worked. Now, I want to take it to the next level when I prompt the user to make a selection from a list of options and based on their input certain part of the code would execute. Here is the revised code:(site: https://locu.com/)

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


# city = input_city = raw_input('Please enter your city: ')
# region = input_region = raw_input('Please enter your state: ')
# zip = raw_input('What is your zip: ')

# 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']
#
# # 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)
---------------------------------------------------------------------------------------------------------------------
The part that is not working is the initial prompt for selection. The program prompts the user to make a selection, however it is not registering that selection rather repeats the prompt.

Hope my question make sense. Please let me know what I am missing. Thank you. Thank you. 


     On Monday, September 28, 2015 9:03 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
   
 

 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


_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  


More information about the Tutor mailing list