{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5a630772",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Getting Input and Converting Types\n",
    "#### CS 65: Introduction to Computer Science I"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcf782f8",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## User Input\n",
    "\n",
    "To receive input from the user, use the built-in `input()` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4d7b282f",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter your name Eric\n",
      "Hello Eric\n"
     ]
    }
   ],
   "source": [
    "name = input(\"Enter your name \")\n",
    "print(\"Hello\",name) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2132cb9e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Think about the `name` variable here. What is its type? How would we check?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9d4d6399",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22739a04",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## User input example: Fahrenheit to Celsius converter\n",
    "\n",
    "The formula for converting Fahrenheit temperatures to Celsius is $$C = (F - 32)*\\frac{5}{9}$$\n",
    "\n",
    "We could make this an interactive program with code like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "52023f88",
   "metadata": {
    "scrolled": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the temperature in Fahrenheit 32\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for -: 'str' and 'int'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-fc29a04c7392>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mfahrenheit_temp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter the temperature in Fahrenheit \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mcelsius_temp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mfahrenheit_temp\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m32\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m9\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"That's\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mcelsius_temp\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"in Celsius\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'str' and 'int'"
     ]
    }
   ],
   "source": [
    "fahrenheit_temp = input(\"Enter the temperature in Fahrenheit \")\n",
    "celsius_temp = (fahrenheit_temp-32)*(5/9)\n",
    "print(\"That's\",celsius_temp,\"in Celsius\") "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdcc970c",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Read the error message! It says we can't subract an int from a string. Even though we typed a number, it treated `fahrenheit_temp` as a string.\n",
    "\n",
    "Unfortunately, that's just the way `input()` works. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3332fc77",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Fortunately, there's another built-in function we can use to convert something to an integer: `int()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "029030d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "val = \"32\"\n",
    "print( type(val) )\n",
    "print( type(int(val)) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "20cb82cc",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the temperature in Fahrenheit 100\n",
      "That's 37.77777777777778 in Celsius\n"
     ]
    }
   ],
   "source": [
    "fahrenheit_temp_str = input(\"Enter the temperature in Fahrenheit \")\n",
    "fahrenheit_temp_int = int(fahrenheit_temp_str)\n",
    "celsius_temp = (fahrenheit_temp_int-32)*(5/9)\n",
    "print(\"That's\",celsius_temp,\"in Celsius\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f59598f",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Or, you can do it all in one step like this"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "70b89ce9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the temperature in Fahrenheit 72\n",
      "That's 22.22222222222222 in Celsius\n"
     ]
    }
   ],
   "source": [
    "fahrenheit_temp = int( input(\"Enter the temperature in Fahrenheit \") )\n",
    "celsius_temp = (fahrenheit_temp-32)*(5/9)\n",
    "print(\"That's\",celsius_temp,\"in Celsius\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad684e89",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "What would happen if the user typed a floating-point value like `72.5`?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3c9decc",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Other type conversion functions\n",
    "\n",
    "Convert to a floating-point type with `float()`\n",
    "\n",
    "Convert to a string with `str()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "425c8fe0",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the temperature in Fahrenheit 72.5\n",
      "That's 22.5 in Celsius\n"
     ]
    }
   ],
   "source": [
    "fahrenheit_temp = float( input(\"Enter the temperature in Fahrenheit \") )\n",
    "celsius_temp = (fahrenheit_temp-32)*(5/9)\n",
    "print(\"That's\",celsius_temp,\"in Celsius\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "af00eebd",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "raw_input() takes from 1 to 2 positional arguments but 3 were given",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-15-120d707f2722>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mtarget_number\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m42\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0muser_input\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter a number greater than \"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mtarget_number\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m)\u001b[0m \u001b[0;31m#doesn't work\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: raw_input() takes from 1 to 2 positional arguments but 3 were given"
     ]
    }
   ],
   "source": [
    "target_number = 42\n",
    "user_input = int( input(\"Enter a number greater than \",target_number) ) #doesn't work"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "db02d0f4",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a number greater than 42 43\n"
     ]
    }
   ],
   "source": [
    "#above code corrected\n",
    "target_number = 42\n",
    "user_input = int( input(\"Enter a number greater than \"+str(target_number)+\" \") )"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
