Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99414 > unrolled thread
| Started by | Scott Montreuil <scott@zml.ca> |
|---|---|
| First post | 2015-11-25 03:45 +0000 |
| Last post | 2015-11-26 13:25 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Scott Montreuil <scott@zml.ca> |
|---|---|
| Date | 2015-11-25 03:45 +0000 |
| Subject | if else python |
| Message-ID | <mailman.51.1448431821.20593.python-list@python.org> |
Hi,
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?
while True:
global latit,longt,jlatit,jlongt,mlongt,mlatit
response = urllib.urlopen(url)
data = json.loads(response.read())
latit = data['Data'][0]['Latitude']
longt = data['Data'][0]['Longitude']
jlatit = data['Data'][1]['Latitude']
jlongt = data['Data'][1]['Longitude']
mlatit = data['Data'][2]['Latitude']
mlongt = data['Data'][2]['Longitude']
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]))
motor1thread = threading.Thread(target=motor1)
motor1thread.start()
screen.refresh()
else:
screen.addstr(5, 40, "%s is at an unknown location %f %f\n" % (user1, latit, longt))
newlocation = 200
motor1thread = threading.Thread(target=motor1)
motor1thread.start()
screen.refresh()
time.sleep(10)
[toc] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-11-25 11:10 +0000 |
| Message-ID | <1555359.vKlKcyBEKv@PointedEars.de> |
| In reply to | #99414 |
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.
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2015-11-26 13:25 +0000 |
| Message-ID | <n3718l$7an$1@reader1.panix.com> |
| In reply to | #99414 |
In <mailman.51.1448431821.20593.python-list@python.org> Scott Montreuil <scott@zml.ca> writes:
> I have an if statement which seems to run both commands and I cannot
> figure out why.
I'm not sure what you mean by "both commands". Do you mean that the
'if' and 'else' branches both execute?
Your if/else branches are in a loop, so perhaps you're seeing the 'if'
branch on one loop iteration, anf the 'else' branch on the next iteration?
I see that your if and else branches both contain a screen.addstr()
call. Are you seeing both of these outputs?
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web