Python - Types of Errors - Syntax Runtime Logical Type Name Value and Indentation Error

preview_player
Показать описание
Everyone makes mistakes, and one of the most common Python mistakes is a syntax error, which is invalid code that the python interpreter doesn’t understand. The interpreter finds syntax errors before you are able to run the program, and gives you some hints on how to fix them. It takes some practice dealing with syntax errors, but eventually you should get the hang of it and fix them quickly.

Ex: print(name); # should not have a semicolon at the end
print(‘hello world”)

It’s good practice to just write a few lines of code, run it and verify that it works, then write a few more lines of code.

Runtime errors

A runtime error is an error that crashes your program while it is running. The Python interpreter does not detect a runtime error because the program's syntax is correct but the program attempts an illegal operation, such as dividing by zero or multiplying strings literals. When a runtime error happens, your program crashes and halts execution.

Ex:
print(‘hello’ * ‘world)
print(‘the end’)

Logic errors

Making a logical mistake in your code, such as multiplying when you should have added is a logic error. The interpreter doesn’t detect logic errors, and the program also doesn’t produce a runtime error and crash. Your program simply just produces the wrong output.

Ex:
sum = 3 * 4
print(‘the sum of 3 and 4 is’, sum)

Other common types of errors

Indentation error is when you didn’t properly indent your code. Ex:
print(‘why did I indent this?’)

Value error is when you pass the wrong value to a function such as int(). Ex:
int(‘five’) # cannot pass letters to int(), only number string literals

Name error is when the program tries to use a variable that does not exist. Ex:
first_name = ‘Bob’
print(last_name)

Type error is when an incorrect type is used, such as adding a string to an integer. Ex:
age = 20 + " years old"

Subscribe to Appficial for more programming videos coming soon. Also, don't forget to click LIKE and comment on the video if it helped you out!
Рекомендации по теме