- Revise the need for branching in programs.
- Consider branching code
- Practice the syntax to create branching code in Python
Must Create simple branching code, which is syntactically correct.
Should complete the above, adding meaningful comments, look at if/else/elif.
Could complete the above,understanding the concept of nested if statements, and also of the advantages of readable code using elif.
Back to Python
When writing a program, we come to a point where we have to make decisions based on data and values within our code.
Now we’re going to build a program to calculate our lifespan. We all know that making healthy choices can prolong our life, while other choices are more harmful. Let’s get the computer to tell us just how much this may affect us.
To begin with, the program will need to find the user’s age. We’ll save this information so that we can then calculate the number of remaining years.
**Remember, input from the keyboard is passed to python as a string, so we’ll need to convert this to a number (integer) if we’re going to use it for calculations.**
Next, the average lifespan for men (79) and women (82) is different, so our program needs to find out if the user is a man or a woman.
Open Python, and copy this code. You’ll need to save it, so call it “life expectancy”:
The first input is easy. We convert the age to an int, and store it in the age variable
The next is tougher. We’re expecting two possibilities, m or f. The program must behave differently depending on the result.
We add this code to the end:
totalYears = 79
if gender == “f”:
This is good, but not quite perfect. The program won’t run if we type anything other than
m or f. The user might make a typing error. If this happens, no value will be added to totalYears, and the program won’t work. one way to solve this problem is the change the second “if” statement (delete the whole line if gender==”f”) to “else:” This is quite useful. The totalYears will be 79 if the user types “n”, and 82 for ANY other input
- If we run this code we don’t know if it works, as we can’t see the value of totalYears. Can you add a line to show us the value and prove that our code works? The line should include a dramatic message like “You have ** years to live”.
- Now a bit of Maths. You need to add a line which changes the value in totalYears. Why? the remaining years is our life expectancy less our age (years we’ve already lived!).
Make sure the line you wrote for question 1 is after this one.
Ok. Did you know that 30 minutes or more of exercise a day adds five years to our life expectancy?
Maybe we should ask how much exercise the user of our program does…
Of course, this is going to be slightly different, because we can only add the five years to the total if the user does more than thirty minutes!
Make sure your program looks like this one:
This is enough, as we don’t change the value for less than thirty minutes of exercise.
**note** in the next to last line, we wanted to take the value already in totalYears, then add 5 to it, and save this new value in totalYears, updating the old value. There are to ways to write this:
totalYears = totalYears +5
totalYears += 5
Finally, what happens if there are various “branches” in our program at once? Imagine this case:
If you eat fast food more than 10 times a month you lose 3 years,
If you eat fast food more than 5 times a month you lose 2 years,
If you eat fast food more than twice a month, you lose 1 years.
Otherwise you don’t lose any time and we print “How healthy :)”
We can create a list of choices like this:
if fastFood >10:
totalYears = totalYears – 3
elif fastFood >5:
totalYears = totalYears – 2
elif fastFood >2:
totalYears = totalYears -1
print(“How healthy :)”
- If your code doesn’t work, make sure the line below an “if” statement is indented (pushed in from the start of the line). You can do this by pressing the TAB key (although it should do it automatically after typing the “if”.
- Your code is about to get very long and hard to read. Make sure you put a few empty lines in between sections of code, so that your program is “readable”
- Your code is also getting quite complex now. You should write “comments”, explaining how your code works. These are typed after the “#” symbol. Python will ignore everything after that symbol.
- Ask the user about fast food, then add the fast food rule to your program, as written above.
- If you smoke, you loose 9 years. Ask the user, and change the total!
- If you eat 5 fruit and veg a day, you gain 7 years.
- If you drink lots of alcohol, you loose 15 years.
- If totalYears is less than 0, the program should print a message: “Your time is up!!”Extension.
- There are many other factors that affect our life expectancy. Conduct some research on-line, and add at least 4 more factors to your program.
- This code gets quite complex and long. Make sure you format (with comments, newlines etc) your code to make it as readable as possible.
- Could this be made any more light/readable/more robust using loops?