Exceptions and Bug Handling
Previous: Python Input-Output | Next: Stateful Parsing |
Required course material for the lesson
Powerpoint: Exceptions and Bug Handling
Video: Exceptions
Video: Exceptions example code
Video: Formatting data
Video: Comparing many values
Video: Finding errors in your program
Video: Playing the guessing game, detecting lies and endings.
Resource: Example code - Exceptions
Subjects covered
How to structure your code in distinct parts.
Finding bugs in your program, using exceptions.
Formatting strings using the methods format, upper and lower.
Exercises to be handed in
- In the file dna.fsa is some DNA. Construct a program that finds possible translation starts :-)
All proteins start with the amino acid methionine (at least when translating, Met might be removed in later processing states). Methionine is coded with ATG. The exercise is therefore; find the position of all ATG's in the sequence. The first position is 83 as humans count.
In some organisms different start codons are possible. If you really want to, you can make the program handle those cases too. - Connecting to the previous exercise; Assuming that the first Met at position 83 is translation start, find the corresponding translation stop (which is the first one in frame). Stop codon is coded by TAA, TAG, or TGA. Remember that the stop codon has to be in the same reading frame as ATG. Notice: There are two ways to solving this exercise. The primitive way is to start at the position given. The more general and better way is to find the first ATG and then find the corresponding stop codon.
- Make a program that asks for an organism, like 'HUMAN' or 'RAT'. The program should then count the number of lines/times a SwissProt identifier in the file orphans.sp is present with said organism, ie. PARG_HUMAN and LUM_HUMAN are the two first for HUMAN. Hint: you should probably think twice in this exercise.
- Count the ATCG's in the sequence in dna.fsa. Display the percentage of ATCG's with 2 digits after the dot. Make it look really structured. Just to be exceedingly clear; the percentage of A's is 100*(NumberOfA/LengthOfSequence).
- Playing time again. Make the guessing program (lesson 4, exercise 4) count how many attempts it needed to guess the number and print it when done guessing. It must be able to detect if you lie (and say so, of course). Also, if you haven't done it before, make the program guess in the fewest possible guesses (an example of 'binary search'). This is what most people naturally do by themselves when they play the game. You 'just' have to do it in the program. There is a system, a method - find it. Hint: it is probably better to start over than reuse your old program.
Exercises for extra practice
- In a file you make yourself are numbers and words with one number/word on each line. Now make a program that reads the file and sums the numbers and displays the result. The words are ignored.
- In the file mixedlines.txt how many 4 letter words exists? A word is here defined as consecutive visible characters, i.e. "12", "ipsum." are both words.
- Write a program that asks for an integer, and then determines if the number was a prime number or not.
- Write a program that prints a "bulls-eye" to the screen using characters like; '.', '+', '*', '#', etc.
You have to import the math library, in order to access the squareroot (sqrt), sine (sin), cosine (cos), and so forth functions. The square root of x is gotten like math.sqrt(x). I only used: for, if, elif, print, +, -, /, **, int, sqrt for this 40x40 example.
Hint: Think of a grid where one point is the center of the eye/circle. The positions in the grid will use different chars, depending how far away they are from the center. You might get an oval shape - think about why.