Exceptions and Bug Handling: Difference between revisions

From 22101
Jump to navigation Jump to search
(Created page with "__NOTOC__ {| width=500 style="float:right; margin-left: 10px; margin-top: -56px;" |Previous: Python Input-Output |Next: Stateful Parsing |} == Required course material for the lesson == Powerpoint: [https://teaching.healthtech.dtu.dk/material/22101/22101_07-Exceptions.ppt Exceptions and Bug Handling]<br> Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=d230fcd7-c06c-414e-a14d-af27012e22f6 Exceptions]<br> Video: [https://panopto.dtu.dk/Panopto/Pages/Vi...")
 
 
(2 intermediate revisions by the same user not shown)
Line 11: Line 11:
Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=08e7ed65-afe3-4f53-9d21-af27012de2bf Comparing many values]<br>
Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=08e7ed65-afe3-4f53-9d21-af27012de2bf Comparing many values]<br>
Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=1255c184-8256-41d9-afc0-af27012dc955 Finding errors in your program]<br>
Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=1255c184-8256-41d9-afc0-af27012dc955 Finding errors in your program]<br>
Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=5e77fe45-be78-4ecf-a5f5-b1f600a24a62 Playing the guessing game], detecting lies and endings.<br>
Resource: [[Example code - Exceptions]]<br>
Resource: [[Example code - Exceptions]]<br>
<!--
<!--
Line 23: Line 24:
== Exercises to be handed in ==
== Exercises to be handed in ==
# In the file ''dna.fsa'' is some DNA. Construct a program that finds possible translation starts :-)<br>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. <br>In some organisms different start codons are possible. If you really want to, you can make the program handle those cases too.
# In the file ''dna.fsa'' is some DNA. Construct a program that finds possible translation starts :-)<br>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. <br>In some organisms different start codons are possible. If you really want to, you can make the program handle those cases too.
# <font color="AA00FF">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.</font>
# <font color="AA00FF">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, which is on position 128.</font>
# <font color="AA00FF">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.</font>
# <font color="AA00FF">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. The position is significant. Check what happens if you search for RAT.</font>
# 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).
# 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 from last week 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.
# 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 ==
== Exercises for extra practice ==

Latest revision as of 09:55, 30 October 2024

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

  1. 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.
  2. 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, which is on position 128.
  3. 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. The position is significant. Check what happens if you search for RAT.
  4. 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).
  5. 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.