{"id":"arithmetic-input-numbers","slug":"numbers-as-input","title":"Numbers as input","text":"`input` always gives a string, even when the user types digits. Calculating with that string as if it were a number fails:\n\n```python\nguests = input(\"Guests: \")\nprint(guests + 2)\n```\n\n```input\n8\n```\n\n```output\nGuests: 8\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 2, in <module>\n    print(guests + 2)\n          ~~~~~~~^~~\nTypeError: can only concatenate str (not \"int\") to str\n```\n\nThe user typed 8, but `guests` holds the string `\"8\"`. With a string on its left, `+` means joining, and Python can't join a number to a string.\n\n### int and float\n\nYou converted numbers into strings with `str`. The functions `int` and `float` go the other way: `int(\"8\")` is the integer 8, and `float(\"2.5\")` is the float 2.5.\n\n```python\nanswer = input(\"Guests: \")\nguests = int(answer)\nprint(f\"Put out {guests + 2} chairs: one for each guest and two spare.\")\n```\n\n```input\n8\n```\n\n```output\nGuests: 8\nPut out 10 chairs: one for each guest and two spare.\n```\n\nPrograms usually do the reading and the converting in one line, with `input` inside the parentheses of `int`. Python works from the inside out: it runs `input` first and hands the string it gets to `int`:\n\n```python\ndog_age = int(input(\"Dog's age in years: \"))\nprint(f\"That's about {dog_age * 7} in human years.\")\n```\n\n```input\n3\n```\n\n```output\nDog's age in years: 3\nThat's about 21 in human years.\n```\n\nWhen the number may have decimals, use `float` instead:\n\n```python\nprice = float(input(\"Price: \"))\nprint(f\"With a 20% discount: {price * 0.8}\")\n```\n\n```input\n12.5\n```\n\n```output\nPrice: 12.5\nWith a 20% discount: 10.0\n```\n\n### Text that isn't a number\n\n`int` only accepts digits, with a minus sign in front if needed. Anything else stops the program with a `ValueError`, even a decimal point:\n\n```python\ncount = int(input(\"How many? \"))\n```\n\n```input\n2.5\n```\n\n```output\nHow many? 2.5\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 1, in <module>\n    count = int(input(\"How many? \"))\nValueError: invalid literal for int() with base 10: '2.5'\n```\n\nThe message says that `int` can't read `'2.5'` as a whole number (\"base 10\" just means ordinary digits). If the user may type decimals, read the number with `float`. A later module shows how a program can deal with input that isn't a number at all.\n","task":"Fill in the blank so the program reads a whole number and doubles it. If the user types `7`, the run looks like this:\n\n```\nNumber: 7\n7 doubled is 14\n```\n","kind":"fill","seed":"number = [[int]](input(\"Number: \"))\nprint(f\"{number} doubled is {number * 2}\")\n","solution":"number = int(input(\"Number: \"))\nprint(f\"{number} doubled is {number * 2}\")\n","hints":["`input` gives a string. Which function turns a string of digits into an integer?","`int(input(\"Number: \"))` reads the line and converts it in one go."],"tests":[{"name":"Test 1","stdin":"7\n","expect":"Number: 7\n7 doubled is 14","hidden":false},{"name":"Test 2","stdin":"-15\n","expect":"Number: -15\n-15 doubled is -30","hidden":true}],"require":[],"forbid":[],"more":[{"task":"If the user types `3`, this program claims that's 3333333 cups a week. When a string is multiplied by a number, Python repeats the string. Fix the program, so that a run looks like this:\n\n```\nCups of coffee a day: 3\nThat's 21 cups a week.\n```\n","kind":"code","seed":"cups = input(\"Cups of coffee a day: \")\nprint(f\"That's {cups * 7} cups a week.\")\n","solution":"cups = int(input(\"Cups of coffee a day: \"))\nprint(f\"That's {cups * 7} cups a week.\")\n","hints":["`cups` holds the string `\"3\"`, not the number 3.","Convert the input with `int` when you read it: `int(input(...))`."],"tests":[{"name":"Test 1","stdin":"3\n","expect":"Cups of coffee a day: 3\nThat's 21 cups a week.","hidden":false},{"name":"Test 2","stdin":"5\n","expect":"Cups of coffee a day: 5\nThat's 35 cups a week.","hidden":true}],"require":[],"forbid":[]},{"task":"Write a program that asks for the price of an item and the number of items, and prints the total price. The price may have decimals, but the number of items is a whole number. If the user types `2.5` and `4`, the run looks like this:\n\n```\nPrice: 2.5\nItems: 4\nTotal: 10.0\n```\n","kind":"code","seed":"# Ask for the price and the number of items, then print the total.\n","solution":"# Ask for the price and the number of items, then print the total.\nprice = float(input(\"Price: \"))\nitems = int(input(\"Items: \"))\nprint(f\"Total: {price * items}\")\n","hints":["The price may have decimals, so read it with `float`. The number of items is whole, so read it with `int`.","The total is the price times the number of items: `print(f\"Total: {price * items}\")`."],"tests":[{"name":"Test 1","stdin":"2.5\n4\n","expect":"Price: 2.5\nItems: 4\nTotal: 10.0","hidden":false},{"name":"Test 2","stdin":"1.25\n3\n","expect":"Price: 1.25\nItems: 3\nTotal: 3.75","hidden":true},{"name":"Test 3","stdin":"8\n2\n","expect":"Price: 8\nItems: 2\nTotal: 16.0","hidden":true}],"require":[],"forbid":[]}]}