So far, you've only seen comparisons where exactly two values are involved. These are called simple conditions . This is probably the most common way to create a condition. But you may find yourself wishing for more power. Luckily, you can combine simple conditions together with logical operators . Combined, these simple conditions become compound conditions . Using compound conditions, your programs can make decisions based on how multiple groups of values compare.
Exclusive clubs are no fun, unless you're a member. So, I created the Exclusive Network program. It simulates an elite computer network where only a select few are
Like real-world computer systems, each person has to enter a username and a password. A member has to enter both his or her username and password, or the member won't be able to log in. With a successful login, the member is
Because I'm not a total elitist, guests are allowed to log in. Guests have the
Figures 3.14 through 3.16 show off the program.
Figure 3.14:
If you're not a member or a guest, you can't get in.
Figure 3.15:
A guest can log in, but their security level is set quite low.
Figure 3.16:
Looks like one of the guys logged in today.
Here's the code:
# Exclusive Network # Demonstrates logical operators conditions # Michael Dawson - 1/3/03 print "\tExclusive Computer Network" print "\t\tMembers only!\n" security = 0 username = "" while not username: username = raw_input("Username: ") password = "" while not password: password = raw_input("Password: ") if username == "M.Dawson" and password == "secret": print "Hi, Mike." security = 5 elif username == "S.Meier" and password == "civilization": print "Hey, Sid." security = 3 elif username == "S.Miyamoto" and password == "mariobros": print "What's up, Shigeru?" security = 3 elif username == "W.Wright" and password == "thesims": print "How goes it, Will?" security = 3 elif username == "guest" or password == "guest": print "Welcome, guest." security = 1 else: print "Login failed. You're not so exclusive.\n" raw_input("\n\nPress the enter key to exit.")
|
|
If you really want to implement a private network, you wouldn't write usernames and passwords directly into your code. You'd probably use some type of
database management system (DBMS).
Database management systems allow you to organize, access, and update
|
|
I wanted to make sure that the user enters something for the username and password. Just pressing the Enter key, which results in the empty string, won't do. I wanted a loop that continues to ask for a username until the
username = "" while not username: username = raw_input("Username: ")
In the
while
condition, I used the logical
not
operator. It works a lot like the word "not." In English,
That means not username is true when username is false. And not username is false when username is true. Here's another way to understand how not works:
|
username |
not username |
|---|---|
|
true |
false |
|
false |
true |
Since
username
is
But when the user finally enters something, username becomes a new string, something other than the empty string. That makes username evaluate to true and not username evaluate to false. As a result, the loop ends, just like I wanted.
The program does the same thing for the variable password .
If a member wants to log in to this exclusive network, the member has to enter a username and password that are recognized together. If, for example, Sid Meier wants to log in, he has to enter S.Meier for his username and civilization for his password. If Sid doesn't enter both, just that way, he can't log in. S.Meier and mariobros won't work. Neither will M.Dawson and civilization . The combination civilization and S.Meier fails too. The program checks that Sid enters S.Meier for his username and civilization for his password with the following code:
elif username == "S.Meier" and password == "civilization":
The line contains a single compound condition made up of two simple conditions. The simple conditions are username == "S.Meier" along with password == "civilization" . These are just like conditions you've already seen, but they've been joined together by the and logical operator to form a larger, compound condition, username == "S.Meier" and password == "civilization" . This compound condition, though longer than you're used to, is still just a condition, which means that it can be either true or false.
So, when is
username == "S.Meier" and password == "civilization"
true, and when is it false? Well, just like in English, "and" means both. So, the condition is true only if both
username == "S.Meier"
and
password == "civilization"
are true;
|
username == "S.Meier" |
password == "civilization" |
username == "S.Meier" and password == "civilization" |
|---|---|---|
|
true |
true |
true |
|
true |
false |
false |
|
false |
true |
false |
|
false |
false |
false |
| HINT |
Put and between two conditions when you want to create a new condition that is true only if both original conditions are true. |
So, when Sid enters S.Meier for his username and civilization for his password, the compound condition is true. Sid is then greeted and assigned a security level.
The program, of course, works for others besides Sid Meier. Through an
If a member or guest doesn't properly log in, the computer prints a "failed login" message and
Guests are allowed in the network, too, but with a limited security level. To make it easy for a guest to try the network, all he or she has to do is enter guest for either the username or password. The following lines of code log in a guest:
elif username == "guest" or password == "guest": print "Welcome, guest." security = 1
The elif condition, username == "guest" or password == "guest" , looks a lot like the other conditions, the ones used for the members. But there's a major difference. The guest condition is created by using the logical or operator.
A compound condition created with an or is true as long as at least one of the simpler conditions is true. Again, the operator works just like in English. "Or" means either, so if either condition is true, the compound condition is true. In this particular case, if username == "guest" is true or if password == "guest" is true, or even if both are true, then username == "guest" or password == "guest" is true; otherwise, it's false. Here's another way to look at how or works:
|
username == "guest" |
password == "guest" |
username == "guest" or password == "guest" |
|---|---|---|
|
true |
true |
true |
|
true |
false |
true |
|
false |
true |
true |
|
false |
false |
false |