Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99438
| Path | csiph.com!news.swapon.de!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: if else python |
| Date | Wed, 25 Nov 2015 11:10:09 +0000 |
| Organization | PointedEars Software (PES) |
| Lines | 66 |
| Message-ID | <1555359.vKlKcyBEKv@PointedEars.de> (permalink) |
| References | <mailman.51.1448431821.20593.python-list@python.org> |
| Reply-To | Thomas 'PointedEars' Lahn <usenet@PointedEars.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| X-Trace | solani.org 1448449810 13592 eJwFwQkBwDAIA0BLhSVQ5PAU/xJ2x8/E2mE0cLnKDGmtGizYL3hYGkdy0HndPLSvI2s1Me8HHfoRXQ== (25 Nov 2015 11:10:10 GMT) |
| X-Complaints-To | abuse@news.solani.org |
| NNTP-Posting-Date | Wed, 25 Nov 2015 11:10:10 +0000 (UTC) |
| User-Agent | KNode/4.14.2 |
| X-User-ID | eJwFwQkBwDAIA0BLK+GVA7TxL2F3Bj++oW6uRuPRrxVAejpx39vCbMR0VqtQcCy6Uqbkls7FRu4lqTLC/AFERRWo |
| Cancel-Lock | sha1:xLsw7NpxqUqOahFuDaRjLleGvQA= |
| X-NNTP-Posting-Host | eJwNx8kBgEAIA8CWCEfEclYi/Zeg85sKgnMli1lbq3ceA1g4naMJaxs5LVOBnX/ee6JdfWP1AR6LER4= |
| Xref | csiph.com comp.lang.python:99438 |
Show key headers only | View raw
Scott Montreuil wrote:
> I have an if statement which seems to run both commands and I cannot
> figure out why. (just learning so I may be missing something obvious) Any
> ideas?
Use a Python IDE and debugger. I recommend PyDev, but YMMV.
> while True:
> global latit,longt,jlatit,jlongt,mlongt,mlatit
Why globals? Why so many?
> response = urllib.urlopen(url)
> data = json.loads(response.read())
> latit = data['Data'][0]['Latitude']
> longt = data['Data'][0]['Longitude']
DRY.
[latit, longt] = [data['Data'][0][key]
for key in ['Latitude', 'Longitude']]
There may be even more efficient ways. Certainly the value of data['Data']
can be stored in a variable.
> jlatit = data['Data'][1]['Latitude']
> jlongt = data['Data'][1]['Longitude']
> mlatit = data['Data'][2]['Latitude']
> mlongt = data['Data'][2]['Longitude']
Shouts out for applying DRY.
[added line continuation escape]
> for i in user1locat:
> if float(i[2]) <= float(latit) <= float(i[3]) and \
> float(i[4]) >= float(longt) >= float(i[5]):
> newlocation = int(i[1])
> screen.addstr(7, 40, "%s at %s" %(user1, i[0]))
["{0} at {1}".format(user1, i[0]) is recommended instead:
<https://docs.python.org/3/library/stdtypes.html#old-string-formatting>]
The indentation of the last quoted line is – as posted – one space to the
left of that of the previous line. That is a syntax error in Python as the
“if” statement is 8 columns to the left instead. Perhaps you have mixed
tabs and spaces and that got lost while posting; in any case, this is not
the original code, so all bets are off.
Also, 8 spaces per indentation level is too much; it certainly is too much
to be posted to Usenet where there is a customary limit at the 80th column.
An indentation of 4 spaces per level is recommended for Python code. If the
code still extends beyond the 80th column, you should use a line
continuation escape sequence if that is syntactically necessary (it is not
in my suggestion above).
<https://www.python.org/dev/peps/pep-0008/#indentation>
--
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
if else python Scott Montreuil <scott@zml.ca> - 2015-11-25 03:45 +0000 Re: if else python Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-11-25 11:10 +0000 Re: if else python John Gordon <gordon@panix.com> - 2015-11-26 13:25 +0000
csiph-web