if ( Parametric )
Also see :
- Startup code
- elif statement (may follow if statement in code)
- else statement (may follow if statement in code)
- Using a text editor
Python objects have truth values. Numbers are true if nonzero. Other objects, such as strings , lists and tuples , are true if nonempty. Consequently, " " and
[ ]
and( )
are each false, whereas"this"
and['that']
and('the', 'other')
are each true. Anif
statement runs the code that is indented beneath it when its condition is true.>>> if 1: ... print(1) ... 1 >>> if "a nonempty string": ... print("a nonempty string":) ... a nonempty string
Of course, both of the
if
statements in the above example are always true and therefore always return the same result. So what do you do for conditions that are sometimes false? To output one result when theif
condition is true and a different result when theif
condition is false, you can add anelse
statement.>>> if 0: ... print(1) ... else: ... print(0) ... 0
The limitation of using only
if
andelse
statements is, of course, that not everything in the world is black and white. To output three or more different results, you can use anif
statement, followed by one or moreelif
statements, and then anelse
statement. Boolean operators such as< > == != <= >=
may be used to make the comparison needed to test a particular conditions. The three numbers that are assigned to the variablenum
and then evaluated in this next example each pass the truth test of only one of theif
,elif
orelse
statements, and each is printed with a distinct print statement that makes it easy for you to understand which one of theif
,elif
orelse
conditions it passed the truth test for.>>> for num in [1, 3, 5]: ... if num < 3: ... print(num, "is less than 3") ... elif num > 3: ... print(num, "is greater than 3") ... else: ... print(num, "is equal to 3") ... 1 is less than 3 3 is equal to 3 5 is greater than 3
In the examples above , the conditions being tested are mutually exclusive, and therefore the
if
andelif
andelse
statements are all in alignment, with the same number of indentation spaces. Anif
statement can also be nested beneath anotherif
statement to test a condition only when theif
condition it is nested under is found to be true. In this next example , theif
statementif num % 2 == 0
, which tests whethernum
is an even number, is nested under the statementif num > 5
. As a result, the numbers 1 through 9 are tested to see if they are even or odd only if they are numbers that are greater than 5. When a number greater than 5 is even, it is appended to theeven
list . All other numbers that are greater than 5 are appended to theodd
list.>>> even = [ ] >>> odd = [ ] >>> for num in [1, 2, 3, 4, 5, 6, 7, 8, 9]: ... if num > 5: ... if num % 2 == 0: ... even.append(num) ... else: ... odd.append(num) ... >>> even [6, 8] >>> odd [7, 9]
Note: The
%
in this example is the modulo operator. It divides the number on its left by the number on its right and returns the remainder.6 % 2
and8 % 2
both return0
since even numbers return a remainder of 0 when divided by 2, and both 6 and 8 are even numbers.
The following script shows the use of if and else statements in a script that tells you the distance from a user-located point and either another user-located point or the 0, 0, 0 global coordinate . Please note that, since Units("feet")
is specified in this script, the output using dim_print()
will be feet-inches fractions
, no matter what the specified primary dimension " Units
" are in setup.
# Distance from user-located point to origin or to other point.
from point import PointLocate, Point
from param import yes_or_no, Units, dim_print
Units("feet") # sets dim_print output to ft-in frac
pt1 = PointLocate("Locate a point")
pt3 = Point(0, 0, 0)
var = yes_or_no("From point or origin?", "point", pt3)
if var is "point":
pt2 = PointLocate("Locate another point")
print(dim_print(pt1.Distance(pt2)))
else:
print(dim_print(pt1.Distance(pt3)))
In this next script, mem1.LeftEnd.Nodes
returns a list -- e.g., [22, 0, 0, 0]
-- which is printed. Lines that are grouped (indented) under the for statement print the member numbers ( mem2.MemberNumber
) and piecemarks ( mem2.Piecemark
) only for those members that are not zero [0]
. The if statement ( if mem_num == 0:
) that is grouped (indented) under the for statement handles the values that are zero in the mem1.LeftEnd.Nodes
list.
# Prints members (pcmk & [num]) supporting the left end of the member that the user selects.
from member import MemberLocate, Member
from param import ClearSelection
mem1 = MemberLocate("Select a member")
print(mem1.Piecemark, "left-end nodes:", mem1.LeftEnd.Nodes)
print("Supporting members:")
for mem_num in mem1.LeftEnd.Nodes:
if mem_num == 0:
continue # go to next node in list, see if it is zero
mem2 = Member(mem_num)
print(mem2.Piecemark, "[",mem2.MemberNumber,"]")
ClearSelection()
This next script uses if statements to cover alternative actions that the user might take. If the user presses the " No " button on the yes-no dialog ( redo =
), the script stops running ( break
). If the user presses " Yes ," the script erases ( mtrl_erase.Erase()
) each rolled section or rectangular/bent plate that the user left-clicks ( Select ) on. When the user presses Esc or right-clicks ( Menu ) and chooses " Cancel " on the dialog, the script ends ( break
). None
is returned when a user cancels a select material operation ( MtrlLocate("prompt string", "Single")
).
# Erases each plate or rolled section that the user selects.
from param import yes_or_no
from mtrl_list import MtrlLocate
from rect_plate import *
from bnt_plate import *
from rolled_section import *
redo = yes_or_no("You must have a material in solid form to run this script. If you do not, press No and re-run the script")
while redo == 1: # user presses "Yes"
if redo == 0: # user presses "No"
break
mtrl = MtrlLocate("Select a material to erase", "Single")
if mtrl == None: #
if user presses Esc
break
else:
mtrl.Erase()
Tip: See Copying and Pasting scripts from Help for information on how to paste these scripts into blank .py text files that you can Run as parametrics in Modeling .