{"id":"return-values-interpreter","slug":"interpreter","title":"The interactive interpreter","text":"So far you have written whole programs: Python reads the file and runs it from top to bottom. Python has a second way of working too, the **interactive interpreter**. You type one line, press Enter, and Python runs it right away and shows the result. Programmers keep one open all the time, to try out a line or two before it goes into a real program.\n\nOn your own computer you start it by typing `python3` (on Windows, `python`) in a terminal. It shows a `>>>` prompt, which means \"type a line of Python\". This site has no interpreter window, but lessons show sessions like this one, with the prompts dimmed:\n\n```pycon\n>>> 7 * 6\n42\n>>> 10 / 4\n2.5\n>>> \"ab\" * 3\n'ababab'\n```\n\n### Values are shown, not printed\n\nWhen the line you type is an expression (something that has a value), the interpreter shows its value, without any `print`. Look at how it shows a string: with quotes. The interpreter shows a value the way you would write it in Python code, so you can tell a string from a number. `print` shows the text itself, without quotes:\n\n```pycon\n>>> word = \"hello\"\n>>> word\n'hello'\n>>> print(word)\nhello\n>>> len(word)\n5\n>>> \"5\"\n'5'\n>>> 5\n5\n```\n\nA line that has no value, such as `word = \"hello\"`, shows nothing. The variable is remembered for the rest of the session, as it would be in a program.\n\nThe difference between `'5'` and `5` matters: the first is a string, the second a number. Later in this module, the checks of many challenges call your functions and show the values they give back in this same way. When a check expects `5` and shows that it got `'5'`, your function gave a string instead of a number.\n\n### Trying out methods\n\nThe interpreter is the quickest way to find out what something does. You've met string **methods** such as `find`: commands that you write after a string, with a dot in between. Strings have many more of them. Two handy ones are `upper` and `lower`, which give the string in capital letters or in small letters:\n\n```pycon\n>>> \"Python Arena\".upper()\n'PYTHON ARENA'\n>>> name = \"ADA\"\n>>> name.lower()\n'ada'\n>>> name\n'ADA'\n>>> name.lowr()\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\nAttributeError: 'str' object has no attribute 'lowr'. Did you mean: 'lower'?\n```\n\nA method gives you a new string: `name` itself is still `'ADA'`. A typo in a method's name gives an `AttributeError`, and when the name is close to a real one, Python even suggests the right spelling. To see the names of all string methods, try `dir(\"\")` in the interpreter. Python uses the names with two underscores at each end behind the scenes; the plain ones, such as `upper`, `lower` and `find`, are the ones you call yourself.\n\nA common use of `lower` is comparing what the user typed without caring about capital letters:\n\n```python\nanswer = input(\"Do you want to continue? \")\nif answer.lower() == \"yes\":\n    print(\"Continuing\")\nelse:\n    print(\"Stopping\")\n```\n\n```input\nYES\n```\n\n```output\nDo you want to continue? YES\nContinuing\n```\n\nWhether the user types `yes`, `Yes` or `YES`, `answer.lower()` is `\"yes\"`.\n\nTo try something out on this site, put it inside `print` in a small program and run it, in any challenge's editor or in the **Playground** (in the menu at the top).\n","task":"Fill in the blanks so the program prints the word the user types, first in capital letters and then in small letters:\n\n```\nWord: PyThOn\nPYTHON\npython\n```\n","kind":"fill","seed":"word = input(\"Word: \")\nprint(word.[[upper]]())\nprint(word.[[lower]]())\n","solution":"word = input(\"Word: \")\nprint(word.upper())\nprint(word.lower())\n","hints":["Each blank is the name of a string method: one gives capital letters, the other small letters.","The method for capital letters is `upper`, and the one for small letters is `lower`."],"tests":[{"name":"Test 1","stdin":"PyThOn\n","expect":"Word: PyThOn\nPYTHON\npython","hidden":false},{"name":"Test 2","stdin":"Hello World\n","expect":"Word: Hello World\nHELLO WORLD\nhello world","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a quiz program. It asks `Which planet is called the red planet? ` until the user answers `mars`, written with any capital letters (`Mars`, `MARS` and `mars` are all right). After each wrong answer it prints `Try again`, and after the right one `Correct!`:\n\n```\nWhich planet is called the red planet? Venus\nTry again\nWhich planet is called the red planet? jupiter\nTry again\nWhich planet is called the red planet? MARS\nCorrect!\n```\n","kind":"code","seed":"# Ask until the answer is mars, in any capital letters.\n","solution":"# Ask until the answer is mars, in any capital letters.\nwhile True:\n    answer = input(\"Which planet is called the red planet? \")\n    if answer.lower() == \"mars\":\n        break\n    print(\"Try again\")\nprint(\"Correct!\")\n","hints":["A `while True:` loop that ends with `break` when the answer is right works well here.","Compare `answer.lower()` with `\"mars\"`: then the capital letters the user typed don't matter.","Print `Try again` inside the loop after a wrong answer, and `Correct!` after the loop."],"tests":[{"name":"Test 1","stdin":"Venus\njupiter\nMARS\n","expect":"Which planet is called the red planet? Venus\nTry again\nWhich planet is called the red planet? jupiter\nTry again\nWhich planet is called the red planet? MARS\nCorrect!","hidden":false},{"name":"Test 2","stdin":"mars\n","expect":"Which planet is called the red planet? mars\nCorrect!","hidden":true},{"name":"Test 3","stdin":"Earth\nMaRs\n","expect":"Which planet is called the red planet? Earth\nTry again\nWhich planet is called the red planet? MaRs\nCorrect!","hidden":true}],"require":[],"forbid":[]},{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"name = \"Grace\"\nprint(name.upper())\nprint(name)\nprint(name.lower() == \"grace\")\nprint(len(name.upper()))\n","solution":"name = \"Grace\"\nprint(name.upper())\nprint(name)\nprint(name.lower() == \"grace\")\nprint(len(name.upper()))\n","hints":["`upper` gives a new string in capital letters. It doesn't change the variable `name`.","`name.lower()` is `\"grace\"`, so the comparison is true, and `print` shows `True`.","The string in capital letters has as many characters as the original."],"tests":[{"name":"Output","stdin":"","expect":"GRACE\nGrace\nTrue\n5","hidden":false}],"require":[],"forbid":[],"lines":["GRACE","Grace","True","5"]}]}