{"id":"printing-errors","slug":"errors","title":"When Python can't read your code","text":"Sooner or later every program has a mistake in it. When Python can't understand a line, it stops and prints an **error message** instead of running the program. Learning to read these messages saves a lot of guessing.\n\n### A syntax error\n\n**Syntax** is the set of rules for how code must be written, like grammar for a language. Here the text is missing its quotes:\n\n```python\nprint(Good morning)\n```\n\n```crash\n  File \"main.py\", line 1\n    print(Good morning)\n          ^^^^^^^^^^^^\nSyntaxError: invalid syntax. Perhaps you forgot a comma?\n```\n\nRead the message from the top:\n\n- `File \"main.py\", line 1` says where the problem is: line 1 of your program (on this site, your program's file is called `main.py`).\n- The next line shows that line of code, and the `^` marks point at the part Python couldn't read.\n- The last line names the kind of error, `SyntaxError`, with Python's guess at the cause. Its guess here (a missing comma) is wrong, which happens: the real problem is the missing quotes. The line number and the marks are usually more reliable than the guess.\n\nWith a syntax error, Python runs nothing at all, not even the lines before the mistake.\n\n### A name Python doesn't know\n\nHere every quote is in place, but the command is misspelled:\n\n```python\nprint(\"Starting...\")\nPrint(\"Hello\")\n```\n\n```output\nStarting...\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 2, in <module>\n    Print(\"Hello\")\n    ^^^^^\nNameError: name 'Print' is not defined. Did you mean: 'print'?\n```\n\nThis time the first line ran and printed `Starting...`, and the program stopped at line 2. A `NameError` means Python doesn't know the name: `Print` with a capital P isn't `print`. The \"Did you mean\" part is often exactly right. On this site, a plain-English note under such a message explains it too.\n","task":"The program has two mistakes, and Python stops at the first one. Fix both, so it prints:\n\n```\nErrors are normal.\nReading them is a skill.\n```\n","kind":"code","seed":"print(\"Errors are normal.)\nPrint(\"Reading them is a skill.\")\n","solution":"print(\"Errors are normal.\")\nprint(\"Reading them is a skill.\")\n","hints":["Press Check and read the first error message. Which line does it name, and what is missing there?","The first string has no closing quote before its `)`.","Once that's fixed, the next message names `Print`. Python's commands are written in small letters."],"tests":[{"name":"Output","stdin":"","expect":"Errors are normal.\nReading them is a skill.","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Fix the program so it prints:\n\n```\nThree lines,\none for each print,\nall in order.\n```\n","kind":"code","seed":"print(\"Three lines,\")\nprint(\"one for each print,\"\nprint(\"all in order.\")\n","solution":"print(\"Three lines,\")\nprint(\"one for each print,\")\nprint(\"all in order.\")\n","hints":["Check it and read the message: Python names the line and says which parenthesis was never closed.","Every opening parenthesis `(` needs a closing one `)`. Count them on line 2."],"tests":[{"name":"Output","stdin":"","expect":"Three lines,\none for each print,\nall in order.","hidden":false}],"require":[],"forbid":[]},{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"# print(\"one\")\nprint(\"two\")\nprint(\"3 + 4\")  # print(3 + 4)\nprint(3 + 4)\n","solution":"# print(\"one\")\nprint(\"two\")\nprint(\"3 + 4\")  # print(3 + 4)\nprint(3 + 4)\n","hints":["A line that starts with `#` is a comment, so Python skips it.","On the third line, everything after the `#` is a comment: only `print(\"3 + 4\")` runs, and it prints text.","The last line has no quotes, so it's calculated."],"tests":[{"name":"Output","stdin":"","expect":"two\n3 + 4\n7","hidden":false}],"require":[],"forbid":[],"lines":["two","3 + 4","7"]}]}