Skip to content
Snippets Groups Projects
python-data-1-recap-sol.ipynb 3.31 KiB
Newer Older
ignat's avatar
ignat committed
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise <a name=\"exercise\"></a>\n",
    "\n",
    "## 1. Fibonacci\n",
    "Create a function `fibonacci()` which takes an integer `num` as an input and returns a list of the fibonacci numbers up to `num`.\n",
    "\n",
    "Eg.\n",
    "\n",
    "Input: `8`\n",
    "\n",
    "Output: `[1, 1, 2, 3, 5, 8, 13, 21]`\n",
    "\n",
    "*Hint: You might want to recall [fibonacci numbers](https://en.wikipedia.org/wiki/Fibonacci_number)*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fibonacci(num):\n",
    "    out = []\n",
    "    for i in range(0,num):\n",
    "        if(i < 2):\n",
    "            out.append(1)\n",
    "        else:\n",
    "            out.append(out[i-1] + out[i-2])\n",
    "    \n",
    "    return out\n",
    "    \n",
    "    \n",
    "\n",
    "################ Checking code ########################\n",
    "# Please don't edit this code\n",
    "newList = fibonacci(10)\n",
    "if newList == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]:\n",
    "    print(\"Success!\")\n",
    "else:\n",
    "    print(\"Error! Your function returned\")\n",
    "    print(newList)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Guessing game\n",
    "Ask the user to input a number and then have the programm guess it. After each guess, the user must input whether it was too high, too low or the correct number. In the end, the program must always guess the users number and it must print out the number of guesses it needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "num = input()\n",
    "guess = 50\n",
    "tries = 0\n",
    "upLimit = 100\n",
    "downLimit = 0\n",
    "\n",
    "while (not(num == guess)):\n",
    "    print(\"I guess\", guess)\n",
    "    isCorrect = input()\n",
    "    if (isCorrect == \"=\"):\n",
    "        print(\"Guessed the number in\", tries, \"tries\")\n",
    "        break\n",
    "    if (isCorrect == \"+\"):\n",
    "        downLimit = guess\n",
    "    elif isCorrect == \"-\":\n",
    "        upLimit = guess\n",
    "    guess = int((upLimit - downLimit)/2 + downLimit)\n",
    "    tries = tries + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "num = int(input())\n",
    "i = 0\n",
    "guess = 50\n",
    "\n",
    "while guess != num:\n",
    "    print(\"Guess the number is\", guess)\n",
    "    feedback = input()\n",
    "    if feedback == \"+\":\n",
    "        guess += 1\n",
    "    elif feedback == \"-\":\n",
    "        guess -= 1\n",
    "    else:\n",
    "        print(\"incorrect number\")\n",
    "    i += 1\n",
    "\n",
    "print(\"Guess your number in\", i, \"tries!\")"
   ]
  }
 ],
 "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.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}