Logic

If Statements

Having Python tell us if something is true or not is fundamental to anything that we want to do in programming. Based on something being true or false, we can tell our code to do something. By using if statements, Python is able to evaluate information and act accordingly.

“If this wall only has three players, I can get it into the top corner. I’ll shoot.”
“If they are playing 3 defenders, I will need to select quick wingers to exploit space.”

Let’s take a look at an example with some numbers.

In [1]:
if 11 > 10:
    print("Done!")
Done!

Simple enough.

If the statement ’11 is greater than 10′ is true, print done. But what if we give something as false?

In [2]:
if 9 > 10:
    print("Done!")

Nothing happens! But we can tell the statement to do something in the case of something false:

In [3]:
if 9 > 10:
    print("Done!")
else:
    print("That was false!")
That was false!

The ‘else:’ statement adds the instructions on what to do if it is false. Please pay close attention to how we structure this. There is a ‘:’ after ‘else’, and a tab space on the new line for the command. This is important!

In 2100, Oldcastle United take the brave step of hiring a robot head coach. Throughout the game, Bobby Roboson decides to tell his players to attack more or less depending on the score. eGaffer may use the following code:

In [4]:
OldcastleScore = 0
OpponentScore = 0

if OldcastleScore < OpponentScore:
    print("ATTACK ATTACK ATTACK!")
else:
    print("10 men behind the ball!")
10 men behind the ball!

So above, if the opponent are ahead, Oldcastle attack. Otherwise, they play ultra-defensively.

We don’t seem to have taken account for a draw. Let’s upgrade iCoach with an ‘elif’ statement – short for else if.

In [5]:
OldcastleScore = 0
OpponentScore = 0

if OldcastleScore < OpponentScore:
    print("ATTACK ATTACK ATTACK!")
elif OldcastleScore == OpponentScore:
    print("Stay solid, but exploit any opportunity!")
else:
    print("10 men behind the ball!")
Stay solid, but exploit any opportunity!

Awesome, our coach has increased his decision making by 50%! It now checks to see if we are losing, if we are drawing or if anything else is happening (which can only be winning). If you change the scores in the example, it will react accordingly. Pretty cool!

Summary

If statements are fundamental to programming – they give machines the ability to react based on information that it receives.

Between ‘if’, ‘elif’ and ‘else’, we gave a coach the ability to assess the scoreline and act accordingly. Great job!

Posted by FCPythonADMIN in Python Basics, 0 comments

Comparisons and Logic

Comparisons and Logic

Every day, we make thousands of tiny judgements that lead to bigger actions. Based on the information available, we will make what we judge to be the right decision. The programs that you will write will be exactly the same – we will feed it information, it will evaluate it and proceed accordingly.

On the pitch, do I take a shot or lay it off for a better chance elsewhere? In the manager’s office, does he sign a new goalkeeper, or show faith in the academy? Decisions are made on the information available – let’s take a look at how this is done in Python.

Comparison Operators

You may be familiar with comparison operators from other programming languages or even in Excel. These are used to ask the relationship between two values. Is one bigger than another? Are they the same?

They will return with either ‘True’ or ‘False’. Eventually, we will write programs that make decisions based on these evaluations.

Let’s use some simple examples with numbers:

In [1]:
# Is four greater than three?
4 > 3
Out[1]:
True
In [2]:
# Is four less than three?
4 < 3
Out[2]:
False
In [3]:
# Is 20 less than or equal to 21?
20 <= 21
Out[3]:
True
In [4]:
# Does 44 equal 44? We use 2 equals signs here!
44 == 44
Out[4]:
True

Remember our variables section? Let’s assign a skill ranking to some players and evaluate them.

In [5]:
# Our skill ranking system assigns two players a ranking, Who is better?

Garrerd = 99
Landard = 98

Garrerd > Landard
Out[5]:
True

Logic Operators

Logic operators allow us to use multiple comparisons together. This is really useful as we can quickly ask more complex questions from our programs.

Let’s take the example above. Why don’t we check if we can afford to purchase the better player, assuming they both cost the same?

In [6]:
# Garrerd = 99 and Landard = 98 as in the last example
# Check that Garrerd is better AND that can we afford them?

Budget = 5000000
PlayerCost = 4000000

(Garrerd > Landard) and (Budget >= PlayerCost)
Out[6]:
True

Sign him up!

By using the ‘and’ keyword, we test if both things are true. If just one has to be true, we can use the ‘or’ keyword instead.

In this new example, our star striker gets a £1,000,000 bonus if she scores more than ten goals or makes at least 15 assists. Let’s test to see if she did it.

In [7]:
HerderGoals = 9
HerderAssists = 16

(HerderGoals > 10) or (HerderAssists >= 15)
Out[7]:
True

Lucky Herder!

Herder gets her bonus! Her teammate Merta, however, is a bit of a loose cannon. Her bonus is given if she scores more than 5 goals, or makes 5 assists AND receives fewer than 3 red cards. Let’s test this.

In [8]:
MertaGoals = 5
MertaAssists = 10
MertaRedCards = 3

(MertaGoals > 5) or ((MertaAssists>=5) and (MertaRedCards<3))
Out[8]:
False

Looks like Merta’s temper has cost her here. While she easily made the number of assists, her red card record let her down.

Pay close attention to how the brackets show Python what the clauses are in our ‘or’ and ‘and’ statements!

Summary

In this piece, we have seen how Python assesses numbers. We can use our operators (<,>, etc.) to ask true or false questions. We can make these even more complex with and/or operators too.

In our basic examples, we used it to quickly check player ratings and affordability. We also used it to check if our players earned their bonuses.

For next steps, we need to tell Python to do something based on these actions. We can do this with if statements in the next article!

Posted by FCPythonADMIN in Python Basics, 0 comments