Thursday, October 9, 2008

Chapter 4 Continued

Since it won’t allow me to go back and edit any of Joseph’s posts this is a continuation of Joseph’s on Chapter 4.

It is possible to have an if statement within another. For example:

sandwich_ingredients = {“ham”:3, “cheese”:2}
fridge_contents = {“ham”:10, “milk”:5, “pepper”:1, “cheese”:3}
have_ingredients = [True]
if fridge_contents[“ham”] > sandwich_ingredients[“ham”]:
have_ingredients[0] = False
have_ingredients.append(“ham”)

if fridge_contents[“cheese”] > sandwich_ingredients[“cheese”]:
if have_ingredients[0] == True:
have_ingredients[0] = False
have_ingredients.append(“cheese”)

In order for the code to continue on to the second if, also known as a nested if, the first one must be true.


That’s all I have for now. I’ll come back later and add while and for loops as well as if-else statements.

No comments: