{"id":"input-variables","slug":"variables","title":"Variables","text":"In the last step, `city = input(...)` stored the user's answer in `city`. A **variable** is a name that holds a value, so the program can use that value later. You create one by writing its name, an equals sign `=` and the value. The value can come from `input`, or you can write it straight into the code:\n\n```python\nlanguage = \"Python\"\nprint(language)\n```\n\n```output\nPython\n```\n\nThe `=` doesn't mean \"is equal to\" as in mathematics. It's an instruction: work out the value on the right, then store it under the name on the left. This is called **assignment**.\n\n### Use it as often as you like\n\nOnce a value is stored, the program can use it again and again. `input` runs only once, and the variable remembers the answer:\n\n```python\nshout = input(\"Shout into the canyon: \")\nprint(shout)\nprint(shout)\nprint(shout)\n```\n\n```input\nhello\n```\n\n```output\nShout into the canyon: hello\nhello\nhello\nhello\n```\n\n### A variable or a piece of text?\n\nQuotes make all the difference here too. Without quotes, `food` is the variable and Python prints its value. With quotes, `\"food\"` is just a string of four letters:\n\n```python\nfood = input(\"Food: \")\nprint(food)\nprint(\"food\")\n```\n\n```input\nsoup\n```\n\n```output\nFood: soup\nsoup\nfood\n```\n\n### Naming variables\n\nYou pick a variable's name yourself, within a few rules:\n\n- Use letters, digits and the underscore `_`, with no spaces, and don't begin with a digit. `room2` is a good name; `2rooms` and `my room` are not names at all.\n- Python tells capital and small letters apart, so `score`, `Score` and `SCORE` would be three separate variables. Use the wrong one, and Python stops with the `NameError` explained below.\n- By convention, Python names are all small letters, with an underscore where a space would go: `pet_name`, `home_town`. This style is called **snake case**.\n\nA good name tells the reader what the value is. Python is just as happy with `x`, but `city` explains itself to anyone who reads the code later, including you a week from now.\n\n### A name Python doesn't know\n\nIf you misspell a variable's name, Python has no variable by that name, so it stops with a `NameError`, the same error a misspelled `print` gives:\n\n```python\ncolor = input(\"Color: \")\nprint(colr)\n```\n\n```input\nred\n```\n\n```output\nColor: red\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 2, in <module>\n    print(colr)\n          ^^^^\nNameError: name 'colr' is not defined. Did you mean: 'color'?\n```\n\nThe variable is `color`, but line 2 asks for `colr`, with a letter missing. Python even suggests the name it knows.\n","task":"This program should ask for a pet's name and print it on a name tag, but it stops with a `NameError`. Fix it, so that a run looks like this:\n\n```\nPet's name: Rex\nHello, my name is\nRex\n```\n","kind":"code","seed":"pet_name = input(\"Pet's name: \")\nprint(\"Hello, my name is\")\nprint(pet_Name)\n","solution":"pet_name = input(\"Pet's name: \")\nprint(\"Hello, my name is\")\nprint(pet_name)\n","hints":["Check it and read the last line of the error message. Which name doesn't Python know?","Capitals matter in names. Compare the name on line 1 with the one on line 3."],"tests":[{"name":"Test 1","stdin":"Rex\n","expect":"Pet's name: Rex\nHello, my name is\nRex","hidden":false},{"name":"Test 2","stdin":"Bella\n","expect":"Pet's name: Bella\nHello, my name is\nBella","hidden":true}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"fruit = \"plum\"\nprint(\"fruit\")\nprint(fruit)\nprint(fruit)\n","solution":"fruit = \"plum\"\nprint(\"fruit\")\nprint(fruit)\nprint(fruit)\n","hints":["The variable `fruit` holds the string `plum`.","The first `print` has quotes around `fruit`, so it prints that word as it is. The other two print the variable's value."],"tests":[{"name":"Output","stdin":"","expect":"fruit\nplum\nplum","hidden":false}],"require":[],"forbid":[],"lines":["fruit","plum","plum"]},{"task":"Python can't even start this program, because one of its variable names breaks the rules. Rename the variable to `home_town` everywhere, so that a run looks like this:\n\n```\nHome town: Bergen\nYou come from\nBergen\n```\n","kind":"code","seed":"home town = input(\"Home town: \")\nprint(\"You come from\")\nprint(home town)\n","solution":"home_town = input(\"Home town: \")\nprint(\"You come from\")\nprint(home_town)\n","hints":["A variable name can't contain a space. Python reads `home town` as two separate names.","Join the words with an underscore, `home_town`, on both lines where the name appears."],"tests":[{"name":"Test 1","stdin":"Bergen\n","expect":"Home town: Bergen\nYou come from\nBergen","hidden":false},{"name":"Test 2","stdin":"Osaka\n","expect":"Home town: Osaka\nYou come from\nOsaka","hidden":true}],"require":[{"pattern":"\\bhome_town\\b","min":2,"message":"Use the name `home_town` both where the variable is created and where it's printed."}],"forbid":[]}]}