There are three broad types of errors that you will encounter as a
computer programmer and today we'll talk about the syntax error. Syntax
errors are usually the easiest to deal with. Python and really all
programming languages are very strict. If things aren't typed in exactly
right, Python is not able to guess what you intend to do. For example as a
human I can look at this string and understand that you want "Please enter
your favorite number" to be inside the string. But if I try to run this
program, Python gives me an error because it looks at this single quote at
the beginning of the string and scan's along to try to find another single
quote at the end of the string to tell it that the string is complete. But
it never finds one. And Python doesn't know that this double quote was
intended to mark the end of the string. So we'll change the beginning
single quote to a double quote. Of course we could have also changed the
double quote to a single quote. And this line is OK. I've got another
problem in my program and if I don't see it right away I can scan over to
the right of the edit window. And hover over this red dash. Often the
error messages aren't very helpful, but at least we can identify where the
error is. In this case we used a semicolon instead of a comma to separate
the arguments in this print function. Another way to find the location of
an error is to go ahead and run the program.
Come down to the error in the run, and click on the hyperlink. And that
takes you to the line where the error occurred. Sometimes you'll see
multiple hyperlinks for one error. Often it helps to click on the last
hyperlink first and then work your way up. When we changed the semicolon
to a comma the error disappears in the edit window and we get our green
check. Running the program shows no problem. Notice that I snuck another
little error in here. I changed the word mean m a i n to main m e i n. But
Python will let me name function's whatever I want. So as long as the
function call down here matches the name of the function up here I won't
get an error. I'd want to fix that anyway.
No comments:
Post a Comment