Brief Overview of Python
Input, output and errors
Find and fix the three kinds of errors.
Input. The input() function lets the user type data: variable = input([Prompt]). The prompt (optional) is shown first. input() takes exactly what is typed and returns it as a string, whether it looks like letters or numbers.
- To use the input as a number, convert it: int() changes a numeric string to an integer, float() to a decimal number. For example, age = int(input("Enter your age: ")). If the text is not a valid number, an error results.
Output. print() evaluates its value and displays it on the screen: print("Hello") shows Hello; print(10*2.5) shows 25.0.
Debugging is identifying and removing errors. There are three kinds:
- Syntax errors break Python's grammar rules: for example, brackets must come in pairs, so (10 + 12) is fine but (7 + 11 is not. The interpreter shows an error and the program does not run until it is fixed.
- Logical (semantic) errors do not stop the program, but it gives wrong output, so they are hard to spot. To average 10 and 12, writing 10 + 12/2 runs and prints 16.0, which is wrong; (10 + 12)/2 gives the correct 11.0.
- Runtime errors stop a program while it is running, even though the statement is grammatically correct, for example dividing by zero.
Example (new): a program asks price = float(input('Price: ')) and qty = int(input('Quantity: ')), then prints price * qty. If the user types 'ten' for the quantity, int() cannot convert it and the program stops with a runtime error.
Practise this lesson
Questions that get harder as you improve, a Tutor that helps without giving the answer away, and progress you can see. Free for every learner.
No account, no email address. Opens the real product.