Using Compound Conditions


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.

Introducing the Exclusive Network Program

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 members. The membership consists of me and several top game designers in the world today (not bad company).

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 personally greeted. Also like real-world systems, everyone has a security level.

Because I'm not a total elitist, guests are allowed to log in. Guests have the lowest security level, though.

Figures 3.14 through 3.16 show off the program.

click to expand
Figure 3.14: If you're not a member or a guest, you can't get in.

click to expand
Figure 3.15: A guest can log in, but their security level is set quite low.

click to expand
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.") 

start sidebar
IN THE REAL WORLD

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 related information. These systems are powerful and could handle thousands or even millions of pairs of usernames and passwords, quickly and securely.

end sidebar

Understanding the not Logical Operator

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 user enters something. This is the loop I came up with for getting the username:

 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, putting the word "not" in front of something creates a new phrase that means the opposite of the old one. In Python, putting not in front of a condition creates a new condition that evaluates to the opposite of the old one.

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 initialized to the empty string in the program, it starts out as false. That makes not username true and the loop runs the first time. Then, the program gets a value for username from the user. If the user just presses Enter, username is the empty string, just as before. And just as before, not username is true and the loop keeps running. So, as long as the user just hits Enter, the loop keeps running, and the user keeps getting prompted for a username.

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.

Understanding the and Logical Operator

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; otherwise it's false. Here's another way to see how this works:

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-elif-else structure, the program checks four different username and password pairs. If a user enters a recognized pair, the member is personally greeted and assigned a security value.

If a member or guest doesn't properly log in, the computer prints a "failed login" message and tells the person that he or she is not so exclusive.

Understanding the or Logical Operator

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




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net