{"id":"input-several","slug":"several-inputs","title":"Several inputs","text":"A program can call `input` as many times as it needs. Each call reads the next line the user types, and each answer usually goes into a variable of its own:\n\n```python\ndish = input(\"Dish: \")\ndrink = input(\"Drink: \")\nprint(\"Your order: \" + dish + \" with \" + drink)\n```\n\n```input\npizza\nlemonade\n```\n\n```output\nDish: pizza\nDrink: lemonade\nYour order: pizza with lemonade\n```\n\nThe answers come in the order the program asks for them. A test on this site types one line for each `input`, in that same order.\n\n### A new value replaces the old one\n\nWhen you store a new value in a variable that already has one, the old value is gone for good. That's fine when you're done with it:\n\n```python\nguess = input(\"First guess: \")\nprint(\"You said \" + guess)\nguess = input(\"Second guess: \")\nprint(\"Now you say \" + guess)\n```\n\n```input\nblue\nred\n```\n\n```output\nFirst guess: blue\nYou said blue\nSecond guess: red\nNow you say red\n```\n\nIt's a bug when you still need the first value. Here both answers go into `place`, so the first one is lost before the program prints it:\n\n```python\nplace = input(\"Where were you born? \")\nplace = input(\"Where do you live now? \")\nprint(\"Born in \" + place)\nprint(\"Living in \" + place)\n```\n\n```input\nOslo\nRome\n```\n\n```output\nWhere were you born? Oslo\nWhere do you live now? Rome\nBorn in Rome\nLiving in Rome\n```\n\nThe fix is a variable for each value you need to keep, such as `birthplace` and `home`.\n\n### Printing several values with commas\n\n`print` can take several values separated by commas. It prints them all on one line, with a space between each two:\n\n```python\nfirst = input(\"First word: \")\nsecond = input(\"Second word: \")\nprint(first, second)\nprint(\"You typed\", first, \"and\", second)\n```\n\n```input\nsun\nflower\n```\n\n```output\nFirst word: sun\nSecond word: flower\nsun flower\nYou typed sun and flower\n```\n\nSo commas add the spaces for you, and `+` adds nothing. `print(\"Hi\", name)` and `print(\"Hi \" + name)` print the same line. When something must come right after a value, with no space, use `+`: `print(name, \"!\")` prints `Ada !`, but `print(name + \"!\")` prints `Ada!`.\n","task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"a = \"sun\"\nb = \"rise\"\nprint(a, b)\nprint(a + b)\nprint(b, a + \"!\")\n","solution":"a = \"sun\"\nb = \"rise\"\nprint(a, b)\nprint(a + b)\nprint(b, a + \"!\")\n","hints":["Commas in `print` put a space between the values. `+` joins strings with nothing between them.","The last line has two values: `b`, and the joined string `a + \"!\"`."],"tests":[{"name":"Output","stdin":"","expect":"sun rise\nsunrise\nrise sun!","hidden":false}],"require":[],"forbid":[],"lines":["sun rise","sunrise","rise sun!"],"more":[{"task":"This program should ask for the names of two players and announce the match, but the first name gets lost. Fix it, so that a run looks like this:\n\n```\nPlayer 1: Ada\nPlayer 2: Linus\nAda vs Linus\n```\n","kind":"code","seed":"player = input(\"Player 1: \")\nplayer = input(\"Player 2: \")\nprint(player + \" vs \" + player)\n","solution":"player1 = input(\"Player 1: \")\nplayer2 = input(\"Player 2: \")\nprint(player1 + \" vs \" + player2)\n","hints":["Both answers are stored in the same variable. What happens to the first answer when the second one arrives?","Give each player a variable of their own, such as `player1` and `player2`, and print both."],"tests":[{"name":"Test 1","stdin":"Ada\nLinus\n","expect":"Player 1: Ada\nPlayer 2: Linus\nAda vs Linus","hidden":false},{"name":"Test 2","stdin":"Mia\nOmar\n","expect":"Player 1: Mia\nPlayer 2: Omar\nMia vs Omar","hidden":true}],"require":[],"forbid":[]},{"task":"Write a program that writes a postcard. It asks for the sender's name, the friend's name and a place, with the prompts shown below, and prints the card. If the user types `Ada`, `Ben` and `Lisbon`, the run looks like this:\n\n```\nYour name: Ada\nFriend's name: Ben\nPlace: Lisbon\nDear Ben,\ngreetings from Lisbon!\nAda\n```\n","kind":"code","seed":"# Ask the three questions, then print the postcard.\n","solution":"# Ask the three questions, then print the postcard.\nsender = input(\"Your name: \")\nfriend = input(\"Friend's name: \")\nplace = input(\"Place: \")\nprint(\"Dear \" + friend + \",\")\nprint(\"greetings from \" + place + \"!\")\nprint(sender)\n","hints":["Ask the three questions in the order shown, each into a variable of its own.","The card has three lines, so three `print` commands. The comma after the friend's name comes right after it, with no space, so join it with `+`.","The first line is `print(\"Dear \" + friend + \",\")` if the friend's name is in `friend`."],"tests":[{"name":"Test 1","stdin":"Ada\nBen\nLisbon\n","expect":"Your name: Ada\nFriend's name: Ben\nPlace: Lisbon\nDear Ben,\ngreetings from Lisbon!\nAda","hidden":false},{"name":"Test 2","stdin":"Kim\nSam\nthe mountains\n","expect":"Your name: Kim\nFriend's name: Sam\nPlace: the mountains\nDear Sam,\ngreetings from the mountains!\nKim","hidden":true}],"require":[],"forbid":[]}]}