Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and try out “while” loops. Most: Experiment with writing their own functions. Some: Write an nth root function.
If there's just one value, you can use empty curly braces. Formatted Printing The format() command makes it easier to produce attractive output. We are going to use this to improve out multiplication table. Here's the new version of the print command. The numbers in curly braces refer to the terms in the format() part of the statement. Starting at zero, each term is slotted in where we want it.
What happens if you don't enter a whole number? It seems a shame that these are not in line with the ones above! Also, what if I want more than one go?
Try these snippets of code and observe what happens. >>> while False: print("False is the new True.") >>> while 6: print("Which numbers are True?") while -1: print("Which numbers are True?") while 0: print("Which numbers are True?") We are going to need another type of loop to let the user have more than one go at the times tables program. Basically, we use a “while” loop when we don't know how many times a loop will need to run. Otherwise, it's usually best to use a “for” loop. Let's experiment with while loops. A “while” loop is basically a condition followed by a block of indented code. The indented block will keep running as long as the condition is True. while
>>> x = 1 >>> while x < 1025: print(x) x *= 2 What does this do? Task: Write a “while” loop which counts down from 10 to 1 in steps of 0.5. Try doing the same in a “for” loop. What happens?
Input Validation In our program, we want the user to enter a whole number. The program tries to turn what they typed into an integer. If this doesn't work, the program crashes. Which is bad. So we can use “try:” and “except:”. These work as you might expect, they try to do the thing we want and let us specify what should happen if it fails. That way the program will not crash (we hope). Enter this program and try it out. It will be a useful reminder of how to use “try:”.
The “while True:” command creates an infinite loop. “try:” / “except:” clauses deal with bad input. We even the output up by adding a space The end = '' bit means “don't add a newline”.
The program now seems to perform better.
Open a new window in IDLE. Save the program as tables2.py Type it in and then run and test it. Python ignores your comments, but they help to make your code readable.
Functions Functions are really important in programming. We have already used some built-in functions like print(). It's very easy to make your own functions in Python. Here's a built-in function: max(). This bit is called a docstring. Here's how to define a function that doubles the number we pass to it.
The docstring explains what the function does. You should always write a docstring for every function you write. A docstring comes right after the “def my_function():” line. A docstring sits within triple quotation marks. The body of the function is indented (Python's IDLE does this for you). Task: Write a function that subtracts one from its input. Include an appropriate docstring and test your function.
Another Simple Function We've seen that we can raise a number to a power using “**”. If we want the square root of a number, we'd have to type something like this : >>> 81** >>> 81**(1/2) 9.0 Task: Write a function that takes two arguments (n and x) and returns the nth root of x.