Saturday, July 30, 2011

Super Simple Python #5 - True, False, and the if statements

If you run out of money, then don't spend money.

Please, keep that in mind. (Not just for this lesson...)

First, True and False are used in Python to denote whether something is... well, true or false. Here's your basic Truth Table:

1 < 2 - True (1 is less than 2)
1 > 2 - False (1 is greater than 2)

Since 1 is NOT greater than 2, you'll get False. Here are the rest (for reference. We'll use them more later):

1 == 1 - True (1 is equal to 1, note the double equal signs)
1 == 2 - False (1 is equal to 2)
1 <= 2 - True (1 is less than OR equal to 2)
1 <= 1 - True (1 is less than OR equal to 1) 
1 >= 2 - False ( 1 is greater than OR equal to 2)
1 >= 1 - True (1 is greater than OR equal to 1)
(x.x) o-(''Q) - Boxing Kirby

If statements work like this:

If some value is True, then do some action.

So in Python, we would write something like this:

>>> my_money = 100
>>> if my_money > 10:
    print('Yay! I have money to spend!')
    my_money = my_money - 10
    print('This is how much money I have left: ', my_money)

Yay! I have money to spend!
This is how much money I have left:  90


We can also add a different statement afterward, called else, that will handle when the other statement didn't happen:

>>> if my_money > 10:
    print('Yay! I have money to spend!')
    my_money = my_money - 10
    print('This is how much money I have left: ', my_money)
else:
    print('I don\'t have enough money!!')
    print('I\'m poor now:', my_money)

    
Yay! I have money to spend!
This is how much money I have left:  80

Let's put that in a function:

>>> def spend_money(my_money, money_to_spend):
    if my_money > money_to_spend:
        print('Yay! I have money to spend!')
        my_money = my_money - money_to_spend
        print('This is how much money I have left: ', my_money)
    else:
        print('I don\'t have enough money!!')
        print('I\'m poor now:', my_money)
    return my_money

Don't forget the return statement!

Now lets go on a shopping spree!

 >>> my_money = spend_money(my_money, 40)
Yay! I have money to spend!
This is how much money I have left:  40

>>> my_money = spend_money(my_money, 20)
Yay! I have money to spend!
This is how much money I have left:  20

>>> my_money = spend_money(my_money, 300)
I don't have enough money!!
I'm poor now: 20

1 comment:

  1. Hey, the output from the program is not quite right.
    from the program:

    >>>def spend_money(my_money, money_to_spend):
    ... if my_money > money_to_spend:
    ... print('Yay! I have money to spend!')
    ... my_money = my_money - money_to_spend
    ... print('This is how much money I have left: ' , my_money)
    ...
    ... else:
    ... print('I don\'t have enough money!!')
    ... print('I\'m poor now:' , my_money)
    ... return my_money

    result (after setting my_money to 180):
    >>> my_money = spend_money(my_money, 40)
    Yay! I have money to spend!
    ('This is how much money I have left: ', 140)

    I'll fiddle with it and see if I can get it to output correctly.

    ReplyDelete