{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 align=\"center\">Control flow</h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "\n",
    "for i in range(7):\n",
    "    generated_number = randrange(-10, 60)\n",
    "    print('Generated number: {}'.format(generated_number))\n",
    "    if generated_number < 0:\n",
    "        continue\n",
    "    if generated_number >= 50:\n",
    "        break\n",
    "    print('\\tUnless it is a single digit, will determine what it is equal to modulo 4')\n",
    "    if generated_number < 10:\n",
    "        pass\n",
    "    elif generated_number % 4 == 1:\n",
    "        print('\\tEqual to one modulo four')\n",
    "    elif generated_number % 4 == 2:\n",
    "        print('\\tEqual to two modulo four')\n",
    "    elif generated_number % 4 == 3:\n",
    "        print('\\tEqual to three modulo four')\n",
    "    else:\n",
    "        print('\\tDivisible by four')\n",
    "else:\n",
    "    print('No number greater than 49 has been generated')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "\n",
    "count_down = 7\n",
    "while count_down:\n",
    "    count_down -= 1\n",
    "    generated_number = randrange(-10, 60)\n",
    "    print('Generated number: {}'.format(generated_number))\n",
    "    if generated_number < 0:\n",
    "        continue\n",
    "    if generated_number >= 50:\n",
    "        break\n",
    "    print('\\tUnless it is a single digit, will determine what it is equal to modulo 4')\n",
    "    if generated_number < 10:\n",
    "        pass\n",
    "    elif generated_number % 4 == 1:\n",
    "        print('\\tEqual to one modulo four')\n",
    "    elif generated_number % 4 == 2:\n",
    "        print('\\tEqual to two modulo four')\n",
    "    elif generated_number % 4 == 3:\n",
    "        print('\\tEqual to three modulo four')\n",
    "    else:\n",
    "        print('\\tDivisible by four')\n",
    "else:\n",
    "    print('No number greater than 49 has been generated')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for x, y in zip((range(5)), (range(5, 0, -1))):\n",
    "    print(x, y, end = '\\t') if x > y else print(y, x, end = '\\t')\n",
    "print('\\n')\n",
    "\n",
    "for x in range(5):\n",
    "    for y in range(5, 0, -1):\n",
    "        print(x, y, end = '\\t') if x > y else print(y, x, end = '\\t')\n",
    "    print()"
   ]
  }
 ],
 "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}

Resource created Wednesday 05 August 2015, 10:53:55 AM, last modified Wednesday 19 August 2015, 10:11:58 AM.

file: control_flow.ipynb


Back to top

COMP9021 15s2 (Principles of Programming) is powered by WebCMS3
CRICOS Provider No. 00098G