Counting calories

Since everyone is concerned about nutrition these days, let's add a calorie count for each combination on the menu. (You might not care about the calories, but I bet your parents do!) That will let us use some of Python's math abilities, which we learned about back in chapter 3.

We already know which items are in each combination. All we need now are the calories for each item. Then we can add them all up in the innermost loop.

Here's some code that sets how many calories are in each item:

dog_cal =

140

bun_cal =

120

mus_cal =

20

ket_cal =

80

onion_cal

= 40

Now we just need to add them up. We know there's either 0 or 1 of each item in each menu combination. So we can just multiply the quantity by the calories for every item, like this: tot_cal = (dog * dog_cal) + (bun * bun_cal) + \

(mustard * mus_cal) + (ketchup * ket_cal) + \ (onion * onion_cal)

Because the order of operations is multiplication first, then addition, I didn't really need to put in the parentheses. I just put them in to make it easier to see what's going on.

Long lines of code

Did you notice the backslash (\) characters at the end of the lines in the previous code? If you have a long expression that won't fit on a single line, you can use the backslash character to tell Python, "This line isn't done. Treat whatever is on the next line as if it's part of this line." Here we used two backslashes to split our long line into three short lines. The backslash is called a line-continuation character, and several programming languages have them.

You can also put an extra set of parentheses around the whole expression, and then you can split your expression over multiple lines without using the backslash, like this:

(mustard * mus_cal) + (ketchup * ket_cal) + (onion * onion_cal))

Putting this all together, the new calorie-counter version of the hot dog program is shown in listing 11.7.

Listing 11.7 Hot dog program with calorie counter dog_cal = 14 0 bun_cal = 12 0 ket_cal = 8 0 mus_cal = 20 onion_cal = 40

Lists calories for each part of the hot dog

Prints print "\tDog \tBun \tKetchup\tMustard\tOnions\tCalories" < headings count = 1 Dog is the for dog in 1]: < outer loop for bun in [0, 1]:

for ketchup in 1]: Nested loops for mustard in [0, 1]:

print "#", count, "\t", print dog, "\t", bun, "\t", ketchup, "\t", print mustard, "\t", onion, print "\t", total_cal Ca|cu|ates ca|°ries count = count + 1

in the inner loop

Try running the program in listing 11.7 in IDLE. The output should look like this:

RESTART

>>>

------ RESTART -----

>>>

Dog

Bun

Ketchup Mustard

Onions

Calories

# 1

0

0

0 0

0

0

# 2

0

0

0 0

1

40

# 3

0

0

0 1

0

20

# 4

0

0

0

1

1

60

# 5

0

0

1

0

0

80

# 6

0

0

1

0

1

120

# 7

0

0

1

1

0

100

# 8

0

0

1

1

1

140

# 9

0

1

0

0

0

120

# 10

0

1

0

0

1

160

# 11

0

1

0

1

0

140

# 12

0

1

0

1

1

180

# 13

0

1

1

0

0

200

# 14

0

1

1

0

1

240

# 15

0

1

1

1

0

220

# 16

0

1

1

1

1

260

# 17

1

0

0

0

0

140

# 18

1

0

0

0

1

180

# 19

1

0

0

1

0

160

# 20

1

0

0

1

1

200

# 21

1

0

1

0

0

220

# 22

1

0

1

0

1

260

# 23

1

0

1

1

0

240

# 24

1

0

1

1

1

280

# 25

1

1

0

0

0

260

# 26

1

1

0

0

1

300

# 27

1

1

0

1

0

280

# 28

1

1

0

1

1

320

# 29

1

1

1

0

0

340

# 30

1

1

1

0

1

380

# 31

1

1

1

1

0

360

# 32

1

1

1

1

1

400

>>>

Just imagine how tedious it would be to work out the calories for all these combinations by hand, even if you had a calculator to do the math. It's way more fun to write a program to figure it all out for you. Looping and a bit of math in Python make it a snap!

0011000111001110000110110100011011010111001100011001100110100 What did you learn?

In this chapter, you learned about

■ permutations and combinations.

0 0

Post a comment