{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Keyboard input to Python\n", "\n", "Python programs can read input from the keyboard. They do this with the function `input()`, as is shown in the following example." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "What's your name? Tim\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Nice to meet you Tim!\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "How old are you? 137\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "So, you are already 137 years old Tim!\n" ] } ], "source": [ "name = input(\"What's your name?\")\n", "print(\"Nice to meet you \" + name + \"!\")\n", "age = input(\"How old are you?\")\n", "print(\"So, you are already\",age,\"years old\",name,\"\\b!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can check the type of the input as below:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Type of name is \n", "Type of age is \n" ] } ], "source": [ "print(\"Type of name is\",type(name))\n", "print(\"Type of age is\",type(age))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Everything (whether numbers or letters) is read as strings. This means the following code will not work (try it!):" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "What's your name? Tim\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Nice to meet you Tim!\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "How old are you? 137\n" ] }, { "ename": "TypeError", "evalue": "unsupported operand type(s) for -: 'int' and 'str'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mage\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"How old are you?\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;31m#\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mretireIn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mretirementAge\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mage\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 8\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mretireIn\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"I guess you will retire in\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mretireIn\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"years,\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"\\b.\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'int' and 'str'" ] } ], "source": [ "retirementAge = 67\n", "#\n", "name = input(\"What's your name?\")\n", "print(\"Nice to meet you \" + name + \"!\")\n", "age = input(\"How old are you?\")\n", "#\n", "retireIn = retirementAge - age\n", "if retireIn > 0:\n", " print(\"I guess you will retire in\",retireIn,\"years,\",name,\"\\b.\")\n", "else:\n", " print(\"I guess you retired\",-retireIn,\"years ago,\",name,\"\\b.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Changing the relevant string's type to `int` or `float` as appropriate fixes the problem." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "What's your name? Tim\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Nice to meet you Tim!\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "How old are you? 137\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "I guess you retired 70 years ago, Tim.\n" ] } ], "source": [ "name = input(\"What's your name?\")\n", "print(\"Nice to meet you \" + name + \"!\")\n", "age = input(\"How old are you?\")\n", "#\n", "retireIn = retirementAge - int(age)\n", "if retireIn > 0:\n", " print(\"I guess you will retire in\",retireIn,\"years,\",name,\"\\b.\")\n", "else:\n", " print(\"I guess you retired\",-retireIn,\"years ago,\",name,\"\\b.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Usually a good idea to check that inputs are sensible. Here we check that the age entered is in the range 0 < age < 120 years. If the age is out of range, we ask that it be re-entered. If an out-of-range answer is given more than 3 times in a row, print an error message and stop the program!" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "What's your name? Tim\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Nice to meet you Tim!\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "How old are you? 137\n", "How old are you? 136\n", "How old are you? 135\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Too many incorrect inputs!\n" ] }, { "ename": "SystemExit", "evalue": "", "output_type": "error", "traceback": [ "An exception has occurred, use %tb to see the full traceback.\n", "\u001b[1;31mSystemExit\u001b[0m\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\green\\Anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py:3327: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n", " warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n" ] } ], "source": [ "import sys\n", "#\n", "# Alternative one\n", "name = input(\"What's your name?\")\n", "print(\"Nice to meet you \" + name + \"!\")\n", "#\n", "count = 0\n", "maxCount = 3\n", "while count < maxCount:\n", " age = input(\"How old are you?\")\n", " if int(age) > 0 and int(age) < 120:\n", " break\n", " count += 1\n", "else:\n", " print(\"Too many incorrect inputs!\")\n", " sys.exit()\n", "#\n", "retireIn = retirementAge - int(age)\n", "if retireIn > 0:\n", " print(\"I guess you will retire in\",retireIn,\"years,\",name,\"\\b.\")\n", "else:\n", " print(\"I guess you retired\",-retireIn,\"years ago,\",name,\"\\b.\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "ename": "SystemExit", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mSystemExit\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Too many incorrect inputs!\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 17\u001b[0m \u001b[1;31m#\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[0mretireIn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mretirementAge\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mage\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mSystemExit\u001b[0m: " ] } ], "source": [ "# The following command will give the last full traceback, only relevant if it was caused by an error at this point!\n", "%tb" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "What's your name? Tim\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Nice to meet you Tim!\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "How old are you? 45\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "I guess you will retire in 22 years, Tim.\n" ] } ], "source": [ "import sys\n", "#\n", "# Alternative two\n", "name = input(\"What's your name?\")\n", "print(\"Nice to meet you \" + name + \"!\")\n", "#\n", "for count in range(0, maxCount):\n", " age = input(\"How old are you?\")\n", " if int(age) > 0 and int(age) < 120:\n", " break\n", "#\n", "if count < maxCount - 1:\n", " retireIn = retirementAge - int(age)\n", " if retireIn > 0:\n", " print(\"I guess you will retire in\",retireIn,\"years,\",name,\"\\b.\")\n", " else:\n", " print(\"I guess you retired\",-retireIn,\"years ago,\",name,\"\\b.\")\n", "else:\n", " print(\"Too many incorrect inputs!\")\n", " sys.exit()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "ename": "SystemExit", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mSystemExit\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Too many incorrect inputs!\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 17\u001b[0m \u001b[1;31m#\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[0mretireIn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mretirementAge\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mage\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mSystemExit\u001b[0m: " ] } ], "source": [ "%tb" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.5" } }, "nbformat": 4, "nbformat_minor": 4 }