[Tutor] Custom Function that Takes argument

Alan Gauld alan.gauld at btinternet.com
Wed Oct 21 05:52:30 EDT 2015


On 21/10/15 01:18, Nym City via Tutor wrote:

> 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']
>
> 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 ")
>        print locu_search(locality)
>        break
>      elif ans=="2":
>          region = raw_input("\n Search by State ")
>          print region_search(region)
>          break
>      elif ans=="3":
>          zip = raw_input("\n Search by Zip ")
>          print zip_search(zip)
>          break
>      elif ans=="4":
>        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
> region_search(region)) I receive the following traceback error:
> Traceback (most recent call last):
>    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




More information about the Tutor mailing list