{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### What is a function?\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* A function is a chunk of code that (usually) takes input, performs operations, then (usually) returns outputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "input = 5.8\n",
    "output=round(input)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def multiply(num1,num2):\n",
    "    product=num1*num2\n",
    "    return product"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = multiply(3,5)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Built-in Python functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Python comes packaged with a lot of useful inbuilt functions\n",
    "* We've used a lot of inbuilt python functions already, e.g. print(), round(), append(), max(), min() etc.\n",
    "* There are also a lot of python modules you can import. These contain very useful functions\n",
    "* For example when you import os you have access to a large range of functions which allow you to manipulate directory and file paths"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "directory = \"./\"\n",
    "\n",
    "for each_item in directory:\n",
    "    print(each_item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "directory = \"./\"\n",
    "\n",
    "for each_item in os.listdir(directory):\n",
    "    print(each_item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### User-defined functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "student_names_grades = {'Azam,Asif': 34.5, 'Bentley,Sara': 90.3, 'King,James': 53.5, 'Li,Fi Fi': 77.9}\n",
    "\n",
    "def get_grade(dict_students,name):\n",
    "    grade = dict_students[name]    \n",
    "    return grade"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_grade(student_names_grades,'Bentley,Sara')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "##Cycle through the values, checking if grade is less than the value. Return the key if true.\n",
    "\n",
    "student_names_grades = {'Azam,Asif': 34.5, 'Bentley,Sara': 85, 'King,James': 53.5, 'Li,Fi Fi': 77.9}\n",
    "performance_bands={'Fail':50,'Pass':65,'Credit':75,'Distinction':85,'High Distinction':100}\n",
    "\n",
    "def get_performance_band(student_names_grades,performance_bands,name):\n",
    "    grade = student_names_grades[name]\n",
    "    for each_band in performance_bands:\n",
    "        if grade < performance_bands[each_band]:\n",
    "            return each_band"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_performance_band(student_names_grades,performance_bands,'Bentley,Sara')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "##More efficient. You only get the value once you have the right key\n",
    "\n",
    "student_names_grades = {'Azam,Asif': 34.5, 'Bentley,Sara': 85, 'King,James': 53.5, 'Li,Fi Fi': 77.9}\n",
    "performance_bands={50:'Fail',65:'Pass',75:'Credit',85:'Distinction',100:'High Distinction'}\n",
    "\n",
    "def get_grade(student_names_grades,performance_bands,name):\n",
    "    grade = student_names_grades[name]\n",
    "    for each_band in performance_bands:\n",
    "        if grade < each_band:\n",
    "            return performance_bands[each_band]\n",
    "    \n",
    "get_grade(student_names_grades,performance_bands,'Bentley,Sara')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "student_names_grades = {'Azam,Asif': 34.5, 'Bentley,Sara': 85, 'King,James': 53.5, 'Li,Fi Fi': 77.9}\n",
    "performance_bands={50:'Fail',65:'Pass',75:'Credit',85:'Distinction',100:'High Distinction'}\n",
    "\n",
    "def show_grades(student_names_grades,performance_bands):\n",
    "    student_performance_bands={}\n",
    "    for student in student_names_grades:\n",
    "        grade = student_names_grades[student]\n",
    "        for each_band in performance_bands:\n",
    "            if grade < each_band:\n",
    "                performance_band=performance_bands[each_band]\n",
    "                student_performance_bands.update({student:performance_band}) #{\"Azam,Asif\":'Fail'} \n",
    "                break #returns control to the outer for loop\n",
    "    return student_performance_bands\n",
    "    \n",
    "show_grades(student_names_grades,performance_bands)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create a Program!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Write a program to generate a student's final grade based on:\n",
    "* 2 assignments, each is worth 30% of the final grade\n",
    "* 1 exam, worth 40% of the final grade\n",
    "* Output a message to showing:\n",
    " * Final grade\n",
    " * Performance band"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Program variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"Asif Azam\"\n",
    "assignment_1 = 10\n",
    "assignment_2 = 30\n",
    "exam = 100\n",
    "\n",
    "weight_mapping = {\"assignment_1\":0.3, \"assignment_2\":0.3,\"exam\":0.4}\n",
    "performance_bands={50:'Fail',65:'Pass',75:'Credit',85:'Distinction',100.001:'High Distinction'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Program functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def moderate_grade(raw_grade, task, weight_mapping):\n",
    "    weighting = weight_mapping[task] #0.3\n",
    "    moderated_grade = weighting*raw_grade #0.3*80\n",
    "    return moderated_grade"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_final_grade(grade_1, grade_2, grade_3): #sum the moderated marks\n",
    "    final_grade = grade_1+grade_2+grade_3\n",
    "    return final_grade"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_performance_band(final_grade, performance_bands):\n",
    "    for each_band in performance_bands:\n",
    "        if final_grade < each_band:\n",
    "            return performance_bands[each_band]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_output(name, final_grade, performance_band):\n",
    "    output = \"{}'s final grade for this unit was: {}%. They achieved a performance band of {}.\".format(name, final_grade, performance_band)\n",
    "    return output"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Bringing it all together!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "moderated_assignment_1 = moderate_grade(assignment_1, \"assignment_1\", weight_mapping)\n",
    "moderated_assignment_2 = moderate_grade(assignment_2, \"assignment_2\", weight_mapping)\n",
    "moderated_exam = moderate_grade(exam, \"exam\",weight_mapping)\n",
    "\n",
    "final_grade = get_final_grade(moderated_assignment_1, moderated_assignment_2, moderated_exam)\n",
    "\n",
    "performance_band = get_performance_band(final_grade, performance_bands)\n",
    "\n",
    "generate_output(name, final_grade, performance_band)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Write a program to generate a student's final grade based on:\n",
    "* 2 assignments, each is worth 30% of the final grade\n",
    "* 1 exam, worth 40% of the final grade\n",
    "* Output a message to each student in a cohort of 10 students, showing:\n",
    " * Final grade\n",
    " * Performance band"
   ]
  }
 ],
 "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.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
