






Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!







[Python eBook]
As you do more and more programming, you will naturally encounter a lot of errors (or bugs ). Causing, understanding, and fixing errors is an important part of programming. Python will do its best to run anything that you tell it to run, but if it can't understand what you're asking, then it won't run the program. All the same, Python will try to tell you a little bit of information about what went wrong, in order to help you try to fix it.
Here are two Python errors.
In this first example, we forget to use the parenthesis that are required by print(...). Python does not understand what you are trying to do.
Here is a second example of a bug in Python.
Run program Program crashed. Error messages: Traceback (most recent call last): In line 1 of the code you submitted: print "Gee golly" ^ SyntaxError: Missing parentheses in call to 'print'. Did you me
print "Gee golly"
In the second example, we forget to define the greeting variable. Python knows what you want it to do, but since no greeting has been defined, an error occurs.
A syntax error happens when Python can't understand what you are saying. A run-time error happens when Python understands what you are saying, but runs into trouble when following your instructions.
In English, a syntax error would be like the sentence
Please cat dog monkey.
The grammar of this sentence does not make sense. From the perspective of English grammar, it is missing a verb (action). You cannot understand what you are being asked to do. Syntax means the same thing as grammar.
In English, a run-time error would be like the sentence
Please eat the piano.
The sentence makes sense from a grammatical perspective — there is a verb and noun in the right place — so you know what you are being asked to do. But, you will encounter problems once you start trying to eat the piano (except maybe if you are a termite). This is called a run-time error because it occurs after the program starts running.
We also will talk about logic errors at the end, which means that your program runs without crashing, but still produces the wrong result. An example would be
Please close the back door so that the bugs don't come in.
This would be a logic error if the front door is also open: even though the command makes sense and you can close the back door, this doesn't actually accomplish the goal of keeping the bugs out since they'll still come in the front door.
Common Syntax Errors in Python
Run program Program crashed. Error messages: Traceback (most recent call last): In line 1 of the code you submitted: print(greeting) NameError: name 'greeting' is not defined
print(greeting)
If you are using quotes around text and you forget the second one, or you are using parentheses and forget the second one, you will get syntax errors:
In this error, EOL is short for E nd O f L ine: Python expected another " but the line ended before it was found.
Similarly, EOF is short for E nd O f F ile: Python kept looking for a ) but the program file ended before it was found.
Sometimes two very similar syntax errors can give two very different error messages. But, every error message is indeed trying to tell you something helpful.
Run-Time Errors
Forgetting the second quote
Run program Program crashed. Error messages: Traceback (most recent call last): In line 1 of the code you submitted: name = "Jim ^ SyntaxError: EOL while scanning string literal
name = "Jim
Forgetting the second parenthesis
Run program Program crashed. Error messages: Traceback (most recent call last): In line 1 of the code you submitted: print( ^ SyntaxError: unexpected EOF while parsing
print(
Here are a few common run-time errors. Python is able to understand what the program says, but runs into problems when actually performing the instructions.
using an undefined variable or function. This can also occur if you use capital letters inconsistently in a variable name:
dividing by zero, which makes no sense in mathematics. (Why? Since 0 times any number is 0, there is no solution to 1 = X * 0, so 1/0 is undefined.)
using operators on the wrong type of data
An undefined variable
Run program Program crashed. Error messages: Traceback (most recent call last): In line 2 of the code you submitted: print(callme) NameError: name 'callme' is not defined
callMe = "Maybe" print(callme)
Dividing by zero
Run program Program crashed. Error messages: Traceback (most recent call last): In line 1 of the code you submitted: print(1/0) ZeroDivisionError: division by zero
print(1/0)
Adding text and a number
Run program
print("you cannot add text and numbers" + 12)
The program with the run-time error created some output, but the one with the syntax error did not. This is because Python runs in two steps:
Logic Errors
Your program might run without crashing (no syntax or run-time errors), but still do the wrong thing. For example, perhaps you want a program to calculate the average of two numbers: the average of x and y is defined as
Why doesn't this program work?
The average should be
SyntaxError: unexpected EOF while parsing
This does not calculate the average correctly.
Run program Program executed without crashing. Program gave the following output:
x = 3 y = 4 average = x + y / 2 print(average)
but the program prints 5.0 instead! The error this time has to do with the "order of operations" in arithmetic. When you write x + y / 2 , this has the same mathematical meaning as. To fix the problem, the
third line of our program should be written as average = (x + y) / 2 , which makes
clear to Python that we really want the value , where we add first and divide
afterwards.
You can have logic errors because you designed a program incorrectly, or because you didn't write code that follows the design correctly (like the average example). Logic errors can be difficult to spot, especially in a longer program, but as you get better at writing code you will also get better at avoiding logic errors. Lesson 6D will give some tips on avoiding logic errors.
Exercises
Now that the lesson is complete, we have three exercises on debugging (fixing errors in programs). You can try to spot the errors before running the programs, or you can run them first and use Python's response to determine what needs fixing.
Each program below comes with some code already written for you. You only need to change a few characters (letters/symbols/numbers) to fix each program. The grader will reject any solution that changes too many characters from the original version. This unusual exercise format is meant to be similar to real-world debugging, where people often accidentally include a typo—can you find and fix the typos rather than redoing the whole solution? Select Reset code to default if you need to bring back the original version.
Fix the syntax error in the following program, so that it prints out the sum of all the numbers from 1 to 10. You can change at most one character.
Run program History Reset code to default Help
print(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 )
Once you have squashed all of the bugs, move on to the next lesson!