Friday, April 21, 2017

If Then, Else?

Last week I said that mathematical operations were the bread and butter of programming, but now, logical operators have become the jelly that's slapped onto it.

This week, as you could probably guess, I started to learn about logic operators! Logic operators are essentially the if-then or if-else statements commonly seen in the majority of programming languages, and are used to evaluate whether a set of conditions are true within a program. These are significant as it is literally used in every program imaginable, and are the basis for modern computers. They're based on the principals of Boolean Algebra, which is a version of algebra were the values of variables are treated as either the true and/or false. Now this was the jumping off point for my knowledge of Ti-Basic and Applesoft BASIC, so please note that some of this information is iffy as I've never had much experience with them.

Now in python an If-Then statement would look something like this;

potato = input("potato")

if potato == "tomato":
    print("potato tomato")

In this statement, if the variable (in if then statements this variable is known as a Sentry Variable) potato is equal to the value tomato, the computer will print the argument "potato tomato". If potato doesn't equate to "tomato", the computer will ignore the statement. If we wanted to, say, have the if-then statement do something if potato isn't "tomato", we can use the else clause:

potato = input("potato")

if potato == "tomato":
    print("potato tomato")
else:
    print("we need a certain red vegetable")

Now if  potato doesn't equate to "tomato", the computer would print "we need a certain red vegetable". If we wanted to have the computer perform actions when potato equates to other values, we can use the elif  (else-if) clause:

potato = input("potato")

if potato == "tomato":
    print("potato tomato")
elif potato == "tornado":
    print("potato tornado")
else:
    print("we need a certain red vegetable or storm-spout thing")

With elif, if potato equates to tornado, the computer will print "potato tornado". If potato doesn't exactly equate to "tomato" or "tornado", the else clause will act as a catch-all for any other values.

If-then statements are great for user input for specific variables, like passwords. The only problem is if we mess up by entering "tamato" rather than "tomato", we can't re-input any new values for potato. The solution is to put the program into a loop, in this case a while loop. A while loop is essentially an if-then/else/else-if statement with if  replaced by while, causing the program to enter a loop until certain conditions are met. If we wanted a program that will loop until "tomato" is entered by adding the while loop, it would look exactly like this:

potato = " "

while potato != "tomato":
    potato = input("we need a certain red vegetable")

print("potato tomato")

The program will now loop until potato is tomato. If potato doesn't equal to tomato, the program will ask the user to enter a different value until tomato is specified. A problem with while loops can occur when the program has a slight flaw in it:

potato = " "

while potato != "tomato":
    potato = print("we need a certain red vegetable")

print("potato tomato")

Output:
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable
we need a certain red vegetable

Did you see the problem? In this case, I accidentally typed a print function rather than a input function, inadvertently causing an infinite loop, printing "we need a certain red vegetable" over and over forever until we manually end it.

We can change how the condition of the sentry variable by using different comparison operators. These are the equal to (=), not equal to (≠), greater than (>), less than (<), greater than or equal to (⋝), and finally the less than or equal to (⋜) symbols that everyone fussed over in elementary school, except they are now are expressed as ==, !=, >, <, >=, <= in python respectively.

Among the logic operators, I also learned about how to generate random numbers using the random module (modules, by the way, is essentially code represented by a certain word). By using this module, we gain the randint() and randrange() functions. The randint() function produces random numbers that are between two specified values (so randint(5,10) would produce a number between 5 and 10), while the randrange() function will produce integers from a specified group of numbers, similar to randint(), but will generate any number between the specified number and 1 (so randrange(12) would generate any number between 1 and 12).

The odd thing about modules though, unlike functions, is that they need to be imported in order to be used. They aren't present by default, so they have to be loaded in using an import statement. The functions in modules also need to be called by using Dot notation, which is a fancy way of saying that you need to type the module's name and the functions name with a period in between.






*Updated. Included more information on logic operators, random integer generators and modules. Also fixed grammatical errors.

bibliography:
Python Programming for the absolute beginner, Third Edition by Michael Dawson

No comments:

Post a Comment