Discussion:
[Tutor] Custom Function that Takes argument
Alan Gauld
2015-09-29 01:03:09 UTC
Permalink
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: ')
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)
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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-10-11 22:25:18 UTC
Permalink
import urllib2
import json
locu_api = 'redacted'
ans=True
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? ")
print("\n Enter City")
print("\n Search by State")
print("\n Search by Zip")
print("\n Goodbye")
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.
# 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
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-10-13 00:30:04 UTC
Permalink
ans=raw_input("What would you like to do? ")
locality = raw_input("\nEnter City ")
break
region = raw_input("\n Search by State ")
break
zip = raw_input("\n Search by Zip ")
break
print("\n Goodbye")
break
... The use case is that by not knowing which of the 4 options the
user will select,
But you know what they select after they have done it so you need to wait
till that point to run the appropriate query. And ans tells you which
choice
the user made.
NameError: name 'locality' is not defined
--------
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.
You didn't. You only define them once the user makes a choice.
eg. If the ans is 3 then locality and region are not defined.

But even if they were (with default values) you don;t want to
run all the queries each time. Only run the query selected by the user.
So call the query functions in the if/else structure where you set the
variables.


HTH
--
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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-10-21 09:52:30 UTC
Permalink
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)
print item['name'], item['phone'], item['street_address'], item['categories'], item['website_url']
ans=True
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? ")
locality = raw_input("\nEnter City ")
print locu_search(locality)
break
region = raw_input("\n Search by State ")
print region_search(region)
break
zip = raw_input("\n Search by Zip ")
print zip_search(zip)
break
print("\n Goodbye")
break
Because you now process the results in the if clauses you no
longer need the breaks. In fact if you want the menu to be
repeated you need to take them all out except for the last one.
-----
I am not sure if above changes exactly reflect your suggestions but I tested the logic and it seems to work.
The only issue is that on the first two queries (locu_search(locality) and
File "C:/Users/dell/Documents/Python/MyProject/API_Projects/locu/locuAPI.py", line 47, in <module>
print locu_search(locality)
File "C:/Users/dell/Documents/Python/MyProject/API_Projects/locu/locuAPI.py", line 14, in locu_search
print item['name'], item['phone'], item['street_address']
File "C:\Python27\lib\encodings\cp1252.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u200e' in position 29: character maps to <undefined>
Looks like you are using cpl252 and your data is coming back as
something else (UTF-8 maybe?) so you probably need to specify
the encoding.
--
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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Continue reading on narkive:
Loading...