<< Chapter < Page | Chapter >> Page > |
However, before you can use a module or the functions contained in a module, you must first import the module into memory. There are a variety of options available when importing modules in Python. You will see an example ofone way to import a module and to use its functions in the next section.
The if statement shown in Listing 1 basically tells the program to test for a condition. If the condition is True, do something special and then continuebusiness as usual. If the condition is False, don't do anything special -- just continue business as usual.
Sometimes our decision processes are more complicated than that. Sometimes we need the program to test for a condition and if the condition is True, theprogram should do something special. However, if the condition is False, the program should do something different that is also special. After one or theother of the special things is done, the program should continue with business as usual. That is the purpose of the if...else statement.
The pseudo-code for a Python if...else statement is shown in Figure 4 .
Figure 4 . Pseudo-code for a Python if...else statement. |
---|
if expression is True:
Execute statements at same indentation levelelse:
Execute different statements at same indentation levelExecute next statement |
As before, the expression must be one that will evaluate to either True or False.
If the expression evaluates to True, the indented statement(s) that follow the expression will be executed in sequence and the indented statement(s) thatfollow else: will be skipped.
If the expression evaluates to False, the indented statement(s) that follow the expression will be skipped and the indented statements that follow else: will be executed.
After that, the next statement following the if statement will be executed.
An example if...else statement is shown in Listing 2 .
Listing 2 . Example of if...else statement usage. |
---|
# Illustrates the if...else statement
#------------------------------------import random
weather = ["sunshine","rain"]rain = False
sunshine = False# Loop until rain and sunshine are both true
while (rain == False or sunshine == False):# Get today's weather
weatherToday = weather[random.randint(0,1)]if weatherToday == "rain":
rain = Trueprint("It's raining, visit the museum.")
else:sunshine = True
print("Sunshine, go to the beach.");print("Vacation is over, go home.") |
The code in Listing 2 simulates a short vacation. You want to visit the museum at least once and go to the beach at least once before yougo home.
The code begins by importing the random module as described earlier . Then it creates and populates three working variables that will be used later.
A while loop is used is used to assure that you visit both the museum and the beach at least once before going home. Note the use of theequality operator ( == ) and the logical ( or ) operator in the conditional clause of the while loop.
The first statement inside the while loop uses the random number generator to get the weather for the day from the list named weather . The result will be either "sunshine" or "rain".
Notification Switch
Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?