Python won't let me call a method
I am making a simple python script that will ask for a zipcode and return the weather implementing the latitude and longitude of that zipcode into a weather.gov url and the weather can be fetched. I am still a beginner so this might be a very simple fix. I will explain my code so far. I have done the Beautiful Soup stuff this is just implementing the zipcode to lat and long.
This imports the search engine:
from uszipcode import SearchEngine
This specifies simple zipcode:
search = SearchEngine(simple_zipcode=True)
This specifies the zipcode in question:
zipcode = search.by_zipcode("11217")
This takes the zipcode, gets it's data, turns it into a dict, and stores it into a variable:
latLong = zipcode.to_dict
This takes the lat and long from the dict and stores them in variables:
lat = latLong['lat'] long = latLong['long']
And finally, this puts the lat and long in the url for BeautifulSoup to do it's magic:
my_url = 'https://forecast.weather.gov/MapClick.php?lat=' + lat + '&lon=' + long + '#.XGTPH1xKiUk'
As you can see, I am using the uszipcode library. (Here's a link: https://uszipcode.readthedocs.io/?badge=latest)
Something else that might be important to note is that this is what you would get if you were to call the latLong variable:
<bound method ExtendedBase.to_dict of SimpleZipcode(zipcode='11217', zipcode_type='Standard', major_city='Brooklyn', post_office_city='Brooklyn, NY', common_city_list=['Brooklyn'], county='Kings County', state='NY', lat=40.68, lng=-73.98, timezone='Eastern', radius_in_miles=0.9090909090909091, area_code_list=['646', '718'], population=35881, population_density=47796.0, land_area_in_sqmi=0.75, water_area_in_sqmi=0.0, housing_units=17581, occupied_housing_units=16269, median_home_value=857000, median_household_income=81862, bounds_west=-73.990867, bounds_east=-73.97064, bounds_north=40.689901, bounds_south=40.672639)>
Ok so here's my problem: Whenever I try to call the method/values of variables in the dictionary, (aka the lat = latLong['lat']) this error message pops up:
Traceback (most recent call last): File "c:\Users\bobko\Desktop\Hello\Test.py", line 8, in <module> lat = zipcode.to_dict['lat'] TypeError: 'method' object is not subscriptable
I don't understand what I'm doing wrong. Can someone please help? Just a side note: I haven't implemented the input thing I am just seeing if this will even work.
You should call the method by placing parentheses after the method name:
latLong = zipcode.to_dict()
Otherwise you're only assigning the method object to
latLong, hence the error.
Good Hunt: 2020 Python Bowl results - Sports, Rookie grand prize winner Kristian Hernandez won $2,000 for an 11-foot, 6.5- inch python. Pro second prize winner Amy Siewe won $750 for a 10� Your Python code can be up on a code editor, IDE or a file. And, it won’t work unless you know how to execute your Python script. In this blog post, we will take a look at 7 ways to execute Python code and scripts. No matter what your operating system is, your Python environment […]
The below works for me.
You have to call the
to_dict method by adding parentheses and also the attribute is not "long" it is "lng"
from uszipcode import SearchEngine search = SearchEngine(simple_zipcode=True) zipcode = search.by_zipcode("11217") latLong = zipcode.to_dict() lat = latLong['lat'] long = latLong['lng']
Python Bowl Winners Killed 80 Invasive Snakes Ahead Of Miami , Tom Rahill, a pro python hunter, wins one of two grand prizes in the Python Bowl, a challenge themed after the Super Bowl. Gerard Albert /� Today the Windows team announced the May 2019 Update for Windows 10.In this post we’re going to look at what we, Microsoft’s Python team, have done to make Python easier to install on Windows by helping the community publish to the Microsoft Store and, in collaboration with Windows, adding a default “python.exe” command to help find it.
zipcode.to_dict is a method. You have to call it, i.e.
Without the parentheses, you're referring to the method instead of calling it.
1. Command line and environment — Python 2.7.18 documentation, If given, Python won't try to write .pyc or .pyo files on the import of source modules . See also PYTHONDONTWRITEBYTECODE . New in version 2.6. -d �. Why Your Baby Ball Python Won’t Eat? Young ball pythons often fail to feed for the same reasons adult ball pythons do (although they rarely fast due to seasonal changes, as adults often do). Accordingly, many of the same techniques that help entice reluctant adults to feed will also work on neonate ball pythons.
1. Command line and environment — Python 3.8.5 documentation, If given, Python won't try to write .pyc files on the import of source modules. See also PYTHONDONTWRITEBYTECODE . --check-hash-based-pycs� Python IDLE will remind you to save whenever you attempt to execute an unsaved file. To execute a file in IDLE, simply press the F5 key on your keyboard. You can also select Run → Run Module from the menu bar. Either option will restart the Python interpreter and then run the code that you’ve written with a fresh interpreter.
Python on Windows FAQ — Python 2.7.18 documentation, How do I make an executable from a Python script? Is a *.pyd file the same as a DLL? How can I embed Python into a Windows� This calls Python 3 into your REPL application and opens up the environment for you. Before you begin your Python programming, you need to understand what a library is. In the non-computer world, a library is a place where different types of books are stored.
Initialization, Finalization, and Threads — Python 3.8.5 documentation, If set to non-zero, Python won't try to write .pyc files on the import of source modules. Set by the -B option and the PYTHONDONTWRITEBYTECODE� C:\Users\hUTBER\AppData\Local\Programs\Python\tools\scripts\win_add2path.py You'll need to make sure that you close and open the cmd otherwise it won't have the new path there. If you can't find this script these are the paths that it will add and I had to add manually in the end.