473,797 Members | 3,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python IF THEN chain equivalence

I'm translating a program in Python that has this IF Then chain
IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN

In other words, as long as 'xi' is less than 'limit' keep going
down the chain, and when 'xi' isn't less than 'limit' jump to end of
chain a continue.

Is this the equivalence in Python?

IF x1 < limit:
--- do a ---
elif x2 < limit:
--- do b ---
----
----
elif x10 < limit:
--- do j ---
Nov 13 '08
15 3242
On Nov 13, 4:39*pm, Grant Edwards <invalid@invali dwrote:
On 2008-11-13, jzakiya <jzak...@mail.c omwrote:
I'm translating a program in Python that has this IF Then chain
IF *x1 < limit: * --- do a ---
* * IF *x2 < limit: *--- do b ---
* * * * IF x3 < limit: *--- do c ---
* * * * * * * * * * * *.-----
* * * * * * * * * * * * ------
* * * * * * * * * * IF *x10 < limt: --- do j ---
* * * * * * * * * * THEN
* * * * * * * * *THEN
* * * * * * * -----
* * * * * THEN
* * *THEN
THEN

The placement of the THEN statements makes absolutely no sense
in any language I've ever seen.
It looks like Visual Basic.

IF a THEN
do_a
IF b THEN
do_b
IF c THEN
do_c
END IF
END IF
END IF

Words different, but same structure.
>
In other words, as long as * *'xi' is less than 'limit' keep going
down the chain, and when 'xi' isn't less than 'limit' jump to end of
chain a continue.
Is this the equivalence in Python?
*IF *x1 < limit:
* * * * --- do a *---
*elif x2 < limit:
* * * * --- do b ---
*----
*----
*elif x10 < limit:
* * * *--- do j ---

No. *That's not the same at all.

Here's one solution:

while True:
* if x1 limit: break
* do a
* if x2 limit: break
* do b
* if x3 limit: break
* do c
* ...
* if x10 limit: break
* do j
* break *

--
Grant Edwards * * * * * * * * * grante * * * * * * Yow! Eisenhower!! *Your
* * * * * * * * * * * * * * * * * at * * * * * * * mimeograph machine upsets
* * * * * * * * * * * * * * * *visi.com * * * * * *my stomach!!
Nov 14 '08 #11
jzakiya wrote:
I'm translating a program in Python that has this IF Then chain
IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN

In other words, as long as 'xi' is less than 'limit' keep going
down the chain, and when 'xi' isn't less than 'limit' jump to end of
chain a continue.
if x1 < limit:
do a
if x2 < limit:
do b
if x3 < limit:
do c
Nov 14 '08 #12
On 2008-11-14, Ethan Furman <et***@stonelea f.uswrote:
jzakiya wrote:
>I'm translating a program in Python that has this IF Then chain
IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN

In other words, as long as 'xi' is less than 'limit' keep going
down the chain, and when 'xi' isn't less than 'limit' jump to end of
chain a continue.

if x1 < limit:
do a
if x2 < limit:
do b
if x3 < limit:
do c
.
.
.
etc
That doesn't do what the OP specified. If any of the
conditions fail, it should "jump" to the end and not execute
_any_ further "do" statements regardless of the values of
subsequent conditions.
On the plus side, it's easy to read and understand -- on the
minus side, it doesn't jump to the end once the tests start
failing.
If all you want is easy to read and understand, then this is
event simpler:
sys.exit(0)

--
Grant

Nov 14 '08 #13
Grant Edwards wrote:
On 2008-11-14, Ethan Furman <et***@stonelea f.uswrote:
>jzakiya wrote:
>>I'm translating a program in Python that has this IF Then chain
IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN

In other words, as long as 'xi' is less than 'limit' keep going
down the chain, and when 'xi' isn't less than 'limit' jump to end of
chain a continue.
if x1 < limit:
do a
if x2 < limit:
do b
if x3 < limit:
do c
.
.
.
etc

That doesn't do what the OP specified. If any of the
conditions fail, it should "jump" to the end and not execute
_any_ further "do" statements regardless of the values of
subsequent conditions.
Ack -- I was aware of the lack of jump, but missed that x7 might be less
than limit even if x6 is greater -- thanks.
>On the plus side, it's easy to read and understand -- on the
minus side, it doesn't jump to the end once the tests start
failing.

If all you want is easy to read and understand, then this is
event simpler:
sys.exit(0)
<sheepish grin>
~ethan~
Nov 14 '08 #14
On Fri, 14 Nov 2008 01:24:32 +0100, M.-A. Lemburg wrote:
>Apparently you haven't seen
any Forth, assembly, et al code. All you're doing is having the branch
point for each conditional be the end of the chain, otherwise it falls
through to the code after the conditional. This is done all the time
in languages that let you actually manipulate the hardware.

Just as a suggestion a little humility would go a long way toward
being open minded and receptive to different paradigms.

Without giving any hint as to what the quoted snippet of code is written
in, how do you expect people to make any sense of it ? Especially when
using an RPN stack oriented language in a Python forum.

There's a reason why we hide Python byte code running on the VM stack
machine from Python users ;-)
It's not like Forth is precisely an obscure little language. For a time,
it was possibly more popular than C. Or predated C? Whatever. I know I
learned about Forth long before I had even heard of C.

Other RPN languages include Postscript, not exactly unheard of either.
Open Firmware is Forth-like, and as you point out yourself, Python byte
code also is a stack-based language.

In conclusion, I'm not sure which is more disappointing: that the OP
couldn't be bothered to mention he was using a Forth-like syntax, or that
so many people failed to recognize it.

Anyway, for what it's worth, here's my translation into Python.

if x1 < limit:
a()
if x2 < limit:
b()
if x3 < limit:
c()
# blah blah blah...
if x10 < limt:
j()
Not very nice code. I think a better way is something like this:

keys = [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10]
functions = [a, b, c, d, e, f, g, h, i, j]

for key, function in zip(keys, functions):
if key < limit:
function()
else:
break

--
Steven
Nov 14 '08 #15
jzakiya wrote:
On Nov 13, 5:48 pm, "M.-A. Lemburg" <m...@egenix.co mwrote:
>On 2008-11-13 23:31, jzakiya wrote:
>>On Nov 13, 5:21 pm, Alan Baljeu <alanbal...@yah oo.comwrote:
I think you should rethink your post. The first case you posted makes no sense in any language I know. Also, a whole lot of nested IF's is a bad idea in any language. In Python, you will end up with code indented 40+ characters if you keep going.
----- Original Message ----
From: jzakiya <jzak...@mail.c om>
To: python-l...@python.org
Sent: Thursday, November 13, 2008 5:06:53 PM
Subject: Python IF THEN chain equivalence
I'm translating a program in Python that has this IF Then chain
IF x1 < limit: --- do a ---
IF x2 < limit: --- do b ---
IF x3 < limit: --- do c ---
.-----
------
IF x10 < limt: --- do j ---
THEN
THEN
-----
THEN
THEN
THEN
In other words, as long as 'xi' is less than 'limit' keep going
down the chain, and when 'xi' isn't less than 'limit' jump to end of
chain a continue.
Is this the equivalence in Python?
IF x1 < limit:
--- do a ---
elif x2 < limit:
--- do b ---
----
----
elif x10 < limit:
--- do j ---
--http://mail.python.org/mailman/listinfo/python-list
_______________ _______________ _______________ _______________ ______
Ask a question on any topic and get answers from real people. Go to Yahoo! Answers and share what you know athttp://ca.answers.yaho o.com
In the code the 'xi's and 'limit' are variables and the --- do letters
---
phrases are simply writes to any array: an_array[xi]=0
Actually, the code makes perfectly good sense, and is a necessity of
the algorithm I'm implementing, and works perfectly good in Forth, and
can be
written quite nicely within a normal page width.
I was just hoping I could perform the equivalent chain in Python
without
having to grossly indent the source code past the normal width of a
printed page.
But if that's the only way to do it in Python, then so be it.
You should probably consider using a function and then
convert the conditions to define return points:

def do_something(.. .args...):

if x1 >= limit:
return
...do a...
if x2 >= limit:
return
...do b...
etc.

That is provided I understand the flow of control in your
example... it's kind of strange to have THEN mark the *end*
of the then-branch in an if-then-else construct.

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Nov 13 2008)>>Python/Zope Consulting and Support ... http://www.egenix.com/
>>>>mxODBC.Zope .Database.Adapt er ... http://zope.egenix.com/
mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
______________ _______________ _______________ _______________ _____________
2008-11-12: Released mxODBC Connect 0.9.3 http://python.egenix.com/

:::: Try mxODBC.Zope.DA for Windows,Linux,S olaris,MacOSX for free ! ::::

eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611

It's interesting to see people think it's strange to have code that
has multiple nested levels of IF THEN. Apparently you haven't seen
any Forth, assembly, et al code. All you're doing is having the branch
point for each conditional be the end of the chain, otherwise it falls
through to the code after the conditional. This is done all the time
in languages that let you actually manipulate the hardware.

Just as a suggestion :-) a little humility would go a long way toward
being open minded and receptive to different paradigms. I've written
this program I'm doing now in Python in 3 other languages (including
Python, which I'm trying to make more efficient) and I seek to be
flexible in my software linguistic capabilities.

I asked a very narrow question about a very specific language
mechanism, and I know exactly what and why I'm doing what I'm doing.
Well in my case what you are doing is pissing me off by failing to
explain your original requirements sufficiently well and then taking a
condescending attitude towards genuine efforts to help by people whose
reputations I know and respect.

One wonders why someone at your stratospheric ability level would even
need any help from the likes of us.

You talk about the need for humility: physician, heal thyself. However
good you may be at software, you demonstrate a marked lack of
interpersonal skills.
I'll try some of the suggestions and see if they make the routine
faster in Python.
That's a good idea.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Nov 17 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

467
21709
by: mike420 | last post by:
THE GOOD: 1. pickle 2. simplicity and uniformity 3. big library (bigger would be even better) THE BAD:
176
8194
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? Thank you, -- greetz tom
41
3561
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of pairs, each pair indicates the sameness of the two indexes. Returns a partitioned list of same indexes.
10
2283
by: Xah Lee | last post by:
another functional exercise with lists. Here's the perl documentation. I'll post a perl and the translated python version in 48 hours. =pod parti(aList, equalFunc) given a list aList of n elements, we want to return a list that is a
12
6838
by: Jay | last post by:
ok, i thought for 2 seconds i might have created a Keylogger in python but i still have one major think stopping me... PYTHON. when i run the program i have python create a file named keylog2.log and it then logs all keys pressed/typed in the python IDE into that file. All i want to know now is how do i hide or background python so that it will log all the keys pressed outside of Python. feel free to play around with my program... but...
2
4455
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
26
2662
by: hide1713 | last post by:
HI I'm currently using Python. I find that a instance variable must confined with self, for example: class a: def __init__(self): self.aa=10 def bb(self): print self.aa # See .if in c++,I could use aa to change that variable
4
1398
by: neptundancer | last post by:
Hi, to extend my skills, I am learning python. I have written small program which computes math expression like "1+2*sin(y^10)/cos(x*y)" and similar, so far only + - * / ^ sin con tan sqrt are supported. But my program is quite inextensible, I have to change the code to add new functions... Could some fellow experienced pythonista give me some tips how to make my program shorter, and more extensible? to use it, try something like...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10214
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.