{"id":"variables-changing","slug":"changing-values","title":"Changing a value","text":"A variable is created the first time a value is stored in it, with `name = value`. In the last module most values came from `input`. A value written straight into the code works the same way, and a new value can be built from other variables:\n\n```python\ngreeting = \"Good morning\"\ncity = \"Lisbon\"\nmessage = greeting + \", \" + city + \"!\"\nprint(message)\n```\n\n```output\nGood morning, Lisbon!\n```\n\nNothing here comes from the user, so the program greets Lisbon on every run until someone edits the code. Values fixed in the code like this are called **hard-coded**.\n\n### A variable can vary\n\nAs its name says, the value of a variable can change. Each assignment replaces the old value, and the variable always holds the latest one:\n\n```python\nstatus = \"waiting\"\nprint(status)\nstatus = \"running\"\nprint(status)\nstatus = \"done\"\nprint(status)\n```\n\n```output\nwaiting\nrunning\ndone\n```\n\n### A new value from the old one\n\nThe new value can be built from the old value of the same variable:\n\n```python\ntitle = input(\"Title: \")\ntitle = \"* \" + title + \" *\"\nprint(title)\ntitle = title + \" (draft)\"\nprint(title)\n```\n\n```input\nSummer plans\n```\n\n```output\nTitle: Summer plans\n* Summer plans *\n* Summer plans * (draft)\n```\n\n`title = \"* \" + title + \" *\"` would be nonsense in mathematics, but remember that `=` is an instruction, not a claim. Python first works out the right side with the value `title` has right now, `\"* \" + \"Summer plans\" + \" *\"`, and only then stores the result back in `title`.\n\n### Each line uses the value of that moment\n\nPython runs the lines in order, and each line uses the values the variables have when that line runs. Changing a variable later doesn't change anything that was already built from it:\n\n```python\npet = \"cat\"\nsentence = \"My \" + pet + \" is asleep.\"\npet = \"dog\"\nprint(sentence)\nprint(pet)\n```\n\n```output\nMy cat is asleep.\ndog\n```\n\n`sentence` was built while `pet` was still `\"cat\"`. From then on it's a finished string: it doesn't remember which variables its parts came from.\n","task":"Fill in the blank so the program adds a title to the name the user types. If the user types `Ada`, the run looks like this:\n\n```\nName: Ada\nAda the Great\n```\n","kind":"fill","seed":"name = input(\"Name: \")\nname = [[name]] + \" the Great\"\nprint(name)\n","solution":"name = input(\"Name: \")\nname = name + \" the Great\"\nprint(name)\n","hints":["The new value is the old value of the same variable with some text joined to its end.","Type the variable's name into the blank: `name = name + \" the Great\"`."],"tests":[{"name":"Test 1","stdin":"Ada\n","expect":"Name: Ada\nAda the Great","hidden":false},{"name":"Test 2","stdin":"Grace\n","expect":"Name: Grace\nGrace the Great","hidden":true}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"food = \"rice\"\nmeal = food + \" and beans\"\nfood = \"pasta\"\nprint(meal)\nprint(food)\n","solution":"food = \"rice\"\nmeal = food + \" and beans\"\nfood = \"pasta\"\nprint(meal)\nprint(food)\n","hints":["`meal` is built on line 2, while `food` is still `rice`.","Changing `food` on line 3 doesn't change `meal`, which is already a finished string."],"tests":[{"name":"Output","stdin":"","expect":"rice and beans\npasta","hidden":false}],"require":[],"forbid":[],"lines":["rice and beans","pasta"]},{"task":"Write a program that asks for a name and then gives it titles, one at a time. Use a single variable `name`: print it, put `Dr. ` in front of it and print it again, then add ` PhD` to its end and print it a third time. If the user types `Ada`, the run looks like this:\n\n```\nName: Ada\nAda\nDr. Ada\nDr. Ada PhD\n```\n","kind":"code","seed":"name = input(\"Name: \")\n","solution":"name = input(\"Name: \")\nprint(name)\nname = \"Dr. \" + name\nprint(name)\nname = name + \" PhD\"\nprint(name)\n","hints":["Print, change, print, change, print: the program has three `print(name)` lines with an assignment between each two.","To put text in front, join it before the old value: `name = \"Dr. \" + name`.","To add text to the end, join it after the old value: `name = name + \" PhD\"`."],"tests":[{"name":"Test 1","stdin":"Ada\n","expect":"Name: Ada\nAda\nDr. Ada\nDr. Ada PhD","hidden":false},{"name":"Test 2","stdin":"Grace\n","expect":"Name: Grace\nGrace\nDr. Grace\nDr. Grace PhD","hidden":true}],"require":[{"pattern":"print\\s*\\(\\s*name\\s*\\)","min":3,"max":3,"message":"Print the variable itself each time, with `print(name)`, and change `name` between the prints."}],"forbid":[]}]}