Exercise 24. More Practice

You are getting to the end of this section. You should have enough Python “under your fingers” to move on to learning about how programming really works, but you should do some more practice. This exercise is longer and all about building up stamina. The next exercise will be similar. Do them, get them exactly right, and do your checks.

ex24.py


 1    print("Let's practice everything.")
 2    print('You'd need to know 'bout escapes with \ that do:')
 3    print(' newlines and tabs.')
 4
 5    poem = """
 6     The lovely world
 7    with logic so firmly planted
 8    cannot discern the needs of love
 9    nor comprehend passion from intuition
10    and requires an explanation
11     where there is none.
12    """
13
14    print("--------------")
15    print(poem)
16    print("--------------")
17
18
19    five = 10 - 2 + 3 - 6
20    print(f"This should be five: {five}")
21
22    def secret_formula(started):
23        jelly_beans = started * 500
24        jars = jelly_beans / 1000
25        crates = jars / 100
26        return jelly_beans, jars, crates
27
28
29    start_point = 10000
30    beans, jars, crates = secret_formula(start_point)
31
32    # remember that this is another way to format a string
33    print("With a starting point of: {}".format(start_point))
34    # it's just like with an f"" string
35    print(f"We'd have {beans} beans, {jars} jars, and {crates} crates.")
36
37    start_point = start_point / 10
38
39    print("We can also do that this way:")
40    formula = secret_formula(start_point)
41    # this is an easy way to apply a list to a format string
42    print("We'd have {} beans, {} jars, and {} crates.".format(*formula))

What You Should See

Exercise 24 Session


$ python3.6 ex24.py
Let's practice everything.
You'd need to know 'bout escapes with that do:

 newlines and      tabs.
--------------

  The lovely world
with logic so firmly planted
cannot discern
 the needs of love
nor comprehend passion from intuition
and requires an explanation

          where there is none.

--------------
This should be five: 5
With a starting point of: 10000
We'd have 5000000 beans, 5000.0 jars, and 50.0 crates.
We can also do that this way:
We'd have 500000.0 beans, 500.0 jars, and 5.0 crates.

Study Drills

1. Make sure to do your checks: read it backward, read it out loud, and put comments above confusing parts.

2. Break the file on purpose, then run it to see what kinds of errors you get. Make sure you can fix it.

Common Student Questions

Why do you call the variable jelly_beans but the name beans later? That’s part of how a function works. Remember that inside the function the variable is temporary. When you return it then it can be assigned to a variable for later. I’m just making a new variable named beans to hold the return value.

What do you mean by reading the code backward? Start at the last line. Compare that line in your file to the same line in mine. Once it’s exactly the same, move up to the next line. Do this until you get to the first line of the file.

Who wrote that poem? I did. Not all of my poems suck.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.41.187