From f8b760be17f5eb0d0f91ab958181dc602d8691dc Mon Sep 17 00:00:00 2001 From: Javier Date: Thu, 5 Dec 2019 13:51:14 +0100 Subject: [PATCH 1/3] adding robot.md to the repository --- robot.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 robot.md diff --git a/robot.md b/robot.md new file mode 100644 index 0000000..e69de29 From 642b95724ffb5b9d48fd4b42d16db8b45bb85a53 Mon Sep 17 00:00:00 2001 From: Javier Hita <58143227+Javierhvb@users.noreply.github.com> Date: Tue, 3 Mar 2020 11:23:20 +0100 Subject: [PATCH 2/3] Add files via upload --- .../1.-Snail-and-Well/snail-and-well.ipynb | 129 ++++++++-- .../duel-of-sorcerers.ipynb | 141 +++++++++-- 1.-Python/3.-Bus/bus.ipynb | 78 +++++- 1.-Python/4.-Robin-Hood/robin-hood.ipynb | 108 +++++++- .../temperature.ipynb | 167 ++++++++++--- .../rock-paper-scissors.ipynb" | 235 +----------------- 6 files changed, 529 insertions(+), 329 deletions(-) diff --git a/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb b/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb index 3402144..b20c5c2 100644 --- a/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb +++ b/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb @@ -30,10 +30,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "well_height = 125\n", + "daily_distance = 30\n", + "nightly_distance = 20\n", + "snail_position = 0\n" + ] }, { "cell_type": "markdown", @@ -44,10 +49,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "days = 0" + ] }, { "cell_type": "markdown", @@ -58,10 +65,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "while snail_position < well_height:\n", + " snail_position += daily_distance - nightly_distance\n", + " days += 1" + ] }, { "cell_type": "markdown", @@ -72,10 +83,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "13\n" + ] + } + ], + "source": [ + "print(days)" + ] }, { "cell_type": "markdown", @@ -96,10 +117,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "13\n" + ] + } + ], + "source": [ + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "while snail_position < well_height:\n", + " for n in advance_cm:\n", + " snail_position += n - nightly_distance\n", + " days += 1\n", + "print(days)" + ] }, { "cell_type": "markdown", @@ -111,10 +147,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "57\n", + "-8\n" + ] + } + ], + "source": [ + "print(max(advance_cm[:days])-20)\n", + "print(min(advance_cm[:days])-20)" + ] }, { "cell_type": "markdown", @@ -125,10 +173,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30.692307692307693\n" + ] + } + ], + "source": [ + "print((sum(advance_cm[:days])-20)/days)" + ] }, { "cell_type": "markdown", @@ -137,6 +195,31 @@ "#### 4. What is the standard deviation of its displacement? Take into account the snail slides at night." ] }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "17.996969441850734\n" + ] + } + ], + "source": [ + "import statistics\n", + "print(statistics.stdev(advance_cm[:days]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -161,7 +244,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.4" } }, "nbformat": 4, diff --git a/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers.ipynb b/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers.ipynb index b4a5f6d..a6f948f 100644 --- a/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers.ipynb +++ b/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers.ipynb @@ -49,10 +49,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf = [10, 11, 13, 30, 22, 11, 10, 33, 22, 22]\n", + "saruman = [23, 66, 12, 43, 12, 10, 44, 23, 12, 17]\n", + "spells = 0" + ] }, { "cell_type": "markdown", @@ -64,10 +68,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_wins = 0\n", + "saruman_wins = 0" + ] }, { "cell_type": "markdown", @@ -78,10 +85,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "while spells < len(gandalf):\n", + " if gandalf[spells] > saruman[spells]:\n", + " gandalf_wins += 1\n", + " elif gandalf[spells] < saruman[spells]:\n", + " saruman_wins += 1\n", + " spells += 1" + ] }, { "cell_type": "markdown", @@ -93,10 +107,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf wins\n" + ] + } + ], + "source": [ + "if gandalf_wins > saruman_wins:\n", + " print('Gandalf wins')\n", + "elif gandalf_wins < saruman_wins:\n", + " print('Saruman wins')\n", + "else:\n", + " print('Tie')" + ] }, { "cell_type": "markdown", @@ -128,10 +157,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "POWER = {'Fireball': 50, 'Lightning bolt': 40, 'Magic arrow': 10, 'Black Tentacles': 25, 'Contagion': 45}\n", + "gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt','Magic arrow','Fireball','Magic arrow', 'Lightning bolt','Fireball', 'Fireball', 'Fireball']\n", + "saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles', 'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']\n", + "spells = 0" + ] }, { "cell_type": "markdown", @@ -142,10 +176,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_wins = 0\n", + "saruman_wins = 0" + ] }, { "cell_type": "markdown", @@ -156,10 +193,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_power = [POWER[p] for p in gandalf]\n", + "saruman_power = [POWER[p] for p in saruman]" + ] }, { "cell_type": "markdown", @@ -171,10 +211,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf_wins\n" + ] + } + ], + "source": [ + "for g , s in zip(gandalf_power, saruman_power):\n", + " if g > s:\n", + " gandalf_wins += 1\n", + " saruman_wins = 0\n", + " if gandalf_wins == 3:\n", + " print('Gandalf_wins')\n", + " break\n", + " elif s > g:\n", + " gandalf_wins = 0\n", + " saruman_wins += 1\n", + " if saruman_wins == 3:\n", + " print('Saruman wins')\n", + " break\n", + "\n", + "if gandalf_wins < 3 and saruman_wins < 3:\n", + " print(\"It's a Tie!\")" + ] }, { "cell_type": "markdown", @@ -185,10 +250,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "39.0\n", + "30.5\n" + ] + } + ], + "source": [ + "print(sum(gandalf_power)/len(gandalf_power))\n", + "print(sum(saruman_power)/len(saruman_power))" + ] }, { "cell_type": "markdown", @@ -197,6 +274,26 @@ "#### 6. Find the standard deviation of the spell power of Gandalf and Saruman. " ] }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15.951314818673865\n", + "16.40629960309962\n" + ] + } + ], + "source": [ + "import statistics\n", + "print(statistics.stdev(gandalf_power))\n", + "print(statistics.stdev(saruman_power))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -221,7 +318,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.4" } }, "nbformat": 4, diff --git a/1.-Python/3.-Bus/bus.ipynb b/1.-Python/3.-Bus/bus.ipynb index 31f09b8..6125316 100755 --- a/1.-Python/3.-Bus/bus.ipynb +++ b/1.-Python/3.-Bus/bus.ipynb @@ -35,7 +35,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -52,10 +52,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n" + ] + } + ], + "source": [ + "print(len(stops))" + ] }, { "cell_type": "markdown", @@ -67,10 +77,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 13, 11, 10, 14, 10, 7, 5, 4]\n" + ] + } + ], + "source": [ + "diff =[(n[0]-n[1]) for n in stops]\n", + "count = []\n", + "current = 0\n", + "for n in diff:\n", + " current += n\n", + " count.append(current)\n", + "print(count)" + ] }, { "cell_type": "markdown", @@ -81,10 +107,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14\n" + ] + } + ], + "source": [ + "print(max(count))" + ] }, { "cell_type": "markdown", @@ -93,6 +129,26 @@ "#### 4. Calculate the average occupation. And the standard deviation." ] }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9.333333333333334\n", + "3.391164991562634\n" + ] + } + ], + "source": [ + "import statistics\n", + "print(sum(count)/len(count))\n", + "print(statistics.stdev(count))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -117,7 +173,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.4" } }, "nbformat": 4, diff --git a/1.-Python/4.-Robin-Hood/robin-hood.ipynb b/1.-Python/4.-Robin-Hood/robin-hood.ipynb index 01de29d..bc216c8 100644 --- a/1.-Python/4.-Robin-Hood/robin-hood.ipynb +++ b/1.-Python/4.-Robin-Hood/robin-hood.ipynb @@ -38,7 +38,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -55,10 +55,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(4, 5), (-3, 2), (5, 7), (5, 7), (2, 2)]\n" + ] + } + ], + "source": [ + "seen = set()\n", + "repeated = []\n", + "for x in points:\n", + " if x in seen:\n", + " repeated.append(x)\n", + " else:\n", + " seen.add(x)\n", + "print(repeated)" + ] }, { "cell_type": "markdown", @@ -70,10 +87,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "In Q1 there are 10 arrows\n", + "In Q2 there are 2 arrows\n", + "In Q3 there are 2 arrows\n", + "In Q4 there are 6 arrows\n" + ] + } + ], + "source": [ + "Q1 = []\n", + "Q2 = []\n", + "Q3 = []\n", + "Q4 = []\n", + "for x in points:\n", + " if x[0] > 0 and x[1] > 0:\n", + " Q1.append(x)\n", + " elif x[0] > 0 and x[1] < 0:\n", + " Q2.append(x)\n", + " elif x[0] < 0 and x[1] < 0:\n", + " Q3.append(x)\n", + " elif x[0] < 0 and x[1] > 0:\n", + " Q4.append(x)\n", + "print(\"In Q1 there are \", len(Q1), \" arrows\")\n", + "print(\"In Q2 there are \", len(Q2), \" arrows\")\n", + "print(\"In Q3 there are \", len(Q3), \" arrows\")\n", + "print(\"In Q4 there are \", len(Q4), \" arrows\")" + ] }, { "cell_type": "markdown", @@ -88,10 +134,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.0\n" + ] + } + ], + "source": [ + "import math\n", + "center = (0, 0)\n", + "def distance (x):\n", + " center_distance = math.sqrt((x[0]**2- center[0]**2)+(x[1]**2-center[0]**2))\n", + " return (center_distance)\n", + "\n", + "points_distance = []\n", + "for n in points:\n", + " points_distance.append(distance(n))\n", + "print(min(points_distance))" + ] }, { "cell_type": "markdown", @@ -101,6 +166,27 @@ "**Hint**: Use the function created in step 3. " ] }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "off_target = 0\n", + "for x in points_distance:\n", + " if x > 9:\n", + " off_target += 1\n", + "print(off_target)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -125,7 +211,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.4" } }, "nbformat": 4, diff --git a/1.-Python/5.-Temperature-Processor/temperature.ipynb b/1.-Python/5.-Temperature-Processor/temperature.ipynb index 4b597aa..fdf6fa1 100644 --- a/1.-Python/5.-Temperature-Processor/temperature.ipynb +++ b/1.-Python/5.-Temperature-Processor/temperature.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -53,10 +53,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "min_temp = min(temperatures_C)" + ] }, { "cell_type": "markdown", @@ -67,10 +69,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "max_temp = max(temperatures_C)" + ] }, { "cell_type": "markdown", @@ -81,10 +85,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "over_70 = []\n", + "for n in temperatures_C:\n", + " if n >= 70:\n", + " over_70.append(n)\n", + " " + ] }, { "cell_type": "markdown", @@ -95,10 +105,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "average = sum(temperatures_C)/len(temperatures_C)" + ] }, { "cell_type": "markdown", @@ -109,10 +121,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "temperatures_C[3] = average" + ] }, { "cell_type": "markdown", @@ -128,10 +142,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "temperatures_F = []\n", + "for n in temperatures_C:\n", + " n = 1.8*n + 32\n", + " temperatures_F.append(n)" + ] }, { "cell_type": "markdown", @@ -150,10 +169,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please, change the cooling system\n" + ] + } + ], + "source": [ + "change = \"Please, change the cooling system\"\n", + "no_problemo = \"There is no need to change the cooling system\"\n", + "if len(over_70) > 3 or max_temp > 80 or average > 65:\n", + " print(change)\n", + "else:\n", + " print(no_problemo)" + ] }, { "cell_type": "markdown", @@ -175,10 +209,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "hours_over_70 = []\n", + "count = 0\n", + "for n in temperatures_C:\n", + " if n >= 70:\n", + " hours_over_70.append(count)\n", + " count += 1" + ] }, { "cell_type": "markdown", @@ -189,10 +230,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Over 4 consecutive temperatures +70 degrees\n" + ] + } + ], + "source": [ + "consecutive_hours = 0\n", + "for n in temperatures_C:\n", + " if consecutive_hours > 4:\n", + " print(\"Over 4 consecutive temperatures +70 degrees\")\n", + " break \n", + " elif n >= 70:\n", + " consecutive_hours += 1\n", + " else:\n", + " consecutive_hours = 0" + ] }, { "cell_type": "markdown", @@ -204,10 +263,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The cooling system needs to be replaced\n" + ] + } + ], + "source": [ + "max_temp = max(temperatures_C)\n", + "average_C = sum(temperatures_C)/len(temperatures_C)\n", + "if consecutive_hours > 4 or average > 65 or max_temp > 85:\n", + " print('The cooling system needs to be replaced')\n", + "else:\n", + " print('The cooling system does not need to be replaced')" + ] }, { "cell_type": "markdown", @@ -218,10 +292,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "62.760416666666664\n", + "144.96875\n" + ] + } + ], + "source": [ + "print(average_C)\n", + "average_F = (average_C * 1.8) + 32\n", + "print(average_F)" + ] }, { "cell_type": "markdown", @@ -230,6 +317,30 @@ "#### 5. Find the standard deviation of the temperature lists (ºC and ºF). What is the relation between both standard deviations?" ] }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14.956727286515202\n", + "26.922109115727363\n" + ] + } + ], + "source": [ + "temperatures_F = []\n", + "for n in temperatures_C:\n", + " n = 1.8*n + 32\n", + " temperatures_F.append(n)\n", + "import statistics\n", + "print(statistics.stdev(temperatures_C))\n", + "print(statistics.stdev(temperatures_F))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -254,7 +365,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.4" } }, "nbformat": 4, diff --git "a/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" index 9e551df..27c430b 100644 --- "a/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" +++ "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" @@ -1,234 +1 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Rock, Paper & Scissors\n", - "\n", - "Let's play the famous game against our computer. You can check the rules [here](https://en.wikipedia.org/wiki/Rock%E2%80%93paper%E2%80%93scissors). \n", - "\n", - "## Task\n", - "Create a program that imitates the playability of the well known game of rock, paper, scissors. Follow the guidelines provided.\n", - "\n", - "## Tools\n", - "1. Loop: **for/while**\n", - "2. Functions: **input(), print()...**\n", - "3. Conditional statements: **if, elif, else**\n", - "4. Definition of functions. Modular programming\n", - "5. Import modules\n", - "\n", - "**To solve this challenge, the use of functions is recommended.**\n", - "\n", - "#### 1. Import the choice function of the random module." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 2. Create a list that includes the 3 possible gesture options of the game: 'rock', 'paper' or 'scissors'. Store the list in a variable called `gestures`." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 3. Create a variable called `n_rounds` to store the maximum number of rounds to play in a game. \n", - "Remember that the number of rounds must be odd: 1, 3, 5, ..." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 4. Create a variable called `rounds_to_win` to store the number of rounds that a player must win to win the game.\n", - "**Hint**: the value stored in `rounds_to_win` depends on the value of `n_rounds`. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 5. Create two variables to store the number of rounds that the computer and the player have won. Call these variables `cpu_score` and `player_score`." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 6. Define a function that randomly returns one of the 3 gesture options.\n", - "You will use this function to simulate the gesture choice of the computer. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 7. Define a function that asks the player which is the gesture he or she wants to show: 'rock', 'paper' or 'scissors'.\n", - "The player should only be allowed to choose one of the 3 gesture options. If the player's choice is not rock, paper or scissors, keep asking until it is." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 8. Define a function that checks who won a round. \n", - "The function should return 0 if there is a tie, 1 if the computer wins and 2 if the player wins." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 9. Define a function that prints the choice of the computer, the choice of the player and a message that announces who won the current round. \n", - "You should also use this function to update the variables that count the number of rounds that the computer and the player have won. The score of the winner increases by one point. If there is a tie, the score does not increase." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 10. Now it's time to code the execution of the game using the functions and variables you defined above. \n", - "\n", - "First, create a loop structure that repeats while no player reaches the minimum score necessary to win and the number of rounds is less than the maximum number of rounds to play in a game. \n", - "\n", - "Inside the loop, use the functions and variables above to create the execution of a round: ask for the player's choice, generate the random choice of the computer, show the round results, update the scores, etc. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 11. Print the winner of the game based on who won more rounds.\n", - "Remember that the game might be tied. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Bonus: Rock, Paper, Scissors, Lizard & Spock\n", - "![](images/rpsls.jpg)\n", - "\n", - "In this challenge, you need to improve the previous game by adding two new options. To know more about the rules of the improved version of rock, paper, scissors, check this [link](http://www.samkass.com/theories/RPSSL.html). \n", - "\n", - "In addition, you will also need to improve how the game interacts with the player: the number of rounds to play, which must be an odd number, will be requested to the user until a valid number is entered. Define a new function to make that request.\n", - "\n", - "**Hint**: Try to reuse the code that you already coded in the previous challenge. If your code is efficient, this bonus will only consist of simple modifications to the original game." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "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.2" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} +{"nbformat":4,"nbformat_minor":0,"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.4"},"colab":{"name":"rock-paper-scissors.ipynb","provenance":[]}},"cells":[{"cell_type":"markdown","metadata":{"id":"NS4f3zHKqE83","colab_type":"text"},"source":[""]},{"cell_type":"markdown","metadata":{"id":"qn-RnY8HqE87","colab_type":"text"},"source":["# Rock, Paper & Scissors\n","\n","Let's play the famous game against our computer. You can check the rules [here](https://en.wikipedia.org/wiki/Rock%E2%80%93paper%E2%80%93scissors). \n","\n","## Task\n","Create a program that imitates the playability of the well known game of rock, paper, scissors. Follow the guidelines provided.\n","\n","## Tools\n","1. Loop: **for/while**\n","2. Functions: **input(), print()...**\n","3. Conditional statements: **if, elif, else**\n","4. Definition of functions. Modular programming\n","5. Import modules\n","\n","**To solve this challenge, the use of functions is recommended.**\n","\n","#### 1. Import the choice function of the random module."]},{"cell_type":"code","metadata":{"id":"iqxdmrWtqE89","colab_type":"code","colab":{}},"source":["import random"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"SWW3REbmqE9E","colab_type":"text"},"source":["#### 2. Create a list that includes the 3 possible gesture options of the game: 'rock', 'paper' or 'scissors'. Store the list in a variable called `gestures`."]},{"cell_type":"code","metadata":{"id":"jicJUVkdqE9G","colab_type":"code","colab":{}},"source":["gestures = [\"Rock\", \"Paper\", \"Scissors\"]"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"ZOjduIkvqE9L","colab_type":"text"},"source":["#### 3. Create a variable called `n_rounds` to store the maximum number of rounds to play in a game. \n","Remember that the number of rounds must be odd: 1, 3, 5, ..."]},{"cell_type":"code","metadata":{"id":"2Yrrtl2JqE9M","colab_type":"code","colab":{}},"source":["n_rounds = 5"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"QpxU9BH8qE9R","colab_type":"text"},"source":["#### 4. Create a variable called `rounds_to_win` to store the number of rounds that a player must win to win the game.\n","**Hint**: the value stored in `rounds_to_win` depends on the value of `n_rounds`. "]},{"cell_type":"code","metadata":{"id":"7CRRGVJ0qE9S","colab_type":"code","colab":{}},"source":["rounds_to_win = 3\n","played_rounds = 0"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"S1RLxpxxqE9X","colab_type":"text"},"source":["#### 5. Create two variables to store the number of rounds that the computer and the player have won. Call these variables `cpu_score` and `player_score`."]},{"cell_type":"code","metadata":{"id":"2P4iRpzHqE9Z","colab_type":"code","colab":{}},"source":["cpu_score = 0\n","player_score = 0"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"B7NspPR7qE9e","colab_type":"text"},"source":["#### 6. Define a function that randomly returns one of the 3 gesture options.\n","You will use this function to simulate the gesture choice of the computer. "]},{"cell_type":"code","metadata":{"id":"GKq82i_TqE9g","colab_type":"code","colab":{}},"source":["def randomly():\n"," global random_cpu\n"," random_cpu = random.choice(gestures)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"v7fPsBotqE9n","colab_type":"text"},"source":["#### 7. Define a function that asks the player which is the gesture he or she wants to show: 'rock', 'paper' or 'scissors'.\n","The player should only be allowed to choose one of the 3 gesture options. If the player's choice is not rock, paper or scissors, keep asking until it is."]},{"cell_type":"code","metadata":{"id":"Y45sSj1pqE9p","colab_type":"code","colab":{}},"source":["def attack():\n"," global gesture \n"," gesture = input(\"Choose Rock, Paper or Scissors: \")\n"," while gesture not in gestures:\n"," gesture = input(\"Invalid Input, Choose Rock Paper or Scissors: \")\n"," "],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"epQaP-hYqE9v","colab_type":"text"},"source":["#### 8. Define a function that checks who won a round. \n","The function should return 0 if there is a tie, 1 if the computer wins and 2 if the player wins."]},{"cell_type":"code","metadata":{"id":"6Wpj5-MSqE9x","colab_type":"code","colab":{}},"source":["def fight():\n"," global gesture\n"," global random_cpu\n"," if gesture == random_cpu:\n"," return 0\n"," elif gesture == \"Rock\":\n"," if random_cpu == \"Paper\":\n"," return 1 \n"," else:\n"," return 2\n"," elif gesture == \"Paper\":\n"," if random_cpu == \"Scissors\":\n"," return 1 \n"," else:\n"," return 2\n"," elif gesture == \"Scissors\":\n"," if random_cpu == \"Rock\":\n"," return 1\n"," else:\n"," return 2"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"OFshmt0bqSrd","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":390},"outputId":"8b506e22-63a7-40d3-b59e-30ff7e5e54c5","executionInfo":{"status":"ok","timestamp":1582723356873,"user_tz":-60,"elapsed":6168,"user":{"displayName":"Javier Hita Vallet-Barceló","photoUrl":"","userId":"08244585397579591248"}}},"source":["!pip install tensorflow"],"execution_count":1,"outputs":[{"output_type":"stream","text":["Requirement already satisfied: tensorflow in /usr/local/lib/python3.6/dist-packages (1.15.0)\n","Requirement already satisfied: protobuf>=3.6.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (3.10.0)\n","Requirement already satisfied: wheel>=0.26 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.34.2)\n","Requirement already satisfied: numpy<2.0,>=1.16.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.17.5)\n","Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.1.0)\n","Requirement already satisfied: gast==0.2.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.2.2)\n","Requirement already satisfied: tensorboard<1.16.0,>=1.15.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.15.0)\n","Requirement already satisfied: google-pasta>=0.1.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.1.8)\n","Requirement already satisfied: keras-applications>=1.0.8 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.0.8)\n","Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (3.1.0)\n","Requirement already satisfied: tensorflow-estimator==1.15.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.15.1)\n","Requirement already satisfied: astor>=0.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.8.1)\n","Requirement already satisfied: wrapt>=1.11.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.11.2)\n","Requirement already satisfied: grpcio>=1.8.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.27.1)\n","Requirement already satisfied: absl-py>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.9.0)\n","Requirement already satisfied: keras-preprocessing>=1.0.5 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.1.0)\n","Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.12.0)\n","Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf>=3.6.1->tensorflow) (45.1.0)\n","Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.6/dist-packages (from tensorboard<1.16.0,>=1.15.0->tensorflow) (3.2.1)\n","Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.6/dist-packages (from tensorboard<1.16.0,>=1.15.0->tensorflow) (1.0.0)\n","Requirement already satisfied: h5py in /usr/local/lib/python3.6/dist-packages (from keras-applications>=1.0.8->tensorflow) (2.8.0)\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"AA-2FgWWqE91","colab_type":"text"},"source":["#### 9. Define a function that prints the choice of the computer, the choice of the player and a message that announces who won the current round. \n","You should also use this function to update the variables that count the number of rounds that the computer and the player have won. The score of the winner increases by one point. If there is a tie, the score does not increase."]},{"cell_type":"code","metadata":{"id":"ofiv3JUWqE92","colab_type":"code","colab":{}},"source":["def winner():\n"," print('You chose ' + gesture)\n"," print('Your opponent chose ' + random_cpu)\n"," global cpu_score\n"," global player_score\n"," if fight() == 1:\n"," cpu_score += 1\n"," print(\"You Lose\")\n"," if fight() == 2:\n"," player_score += 1\n"," print(\"You Win!\")\n"," if fight() == 0:\n"," print(\"It's a Tie\")\n"," "],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"7vmR5GZUqE96","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"rLTUICAPqE9_","colab_type":"text"},"source":["#### 10. Now it's time to code the execution of the game using the functions and variables you defined above. \n","\n","First, create a loop structure that repeats while no player reaches the minimum score necessary to win and the number of rounds is less than the maximum number of rounds to play in a game. \n","\n","Inside the loop, use the functions and variables above to create the execution of a round: ask for the player's choice, generate the random choice of the computer, show the round results, update the scores, etc. "]},{"cell_type":"code","metadata":{"id":"HVzvfATnqE-B","colab_type":"code","colab":{}},"source":["while played_rounds < n_rounds and player_score < rounds_to_win and cpu_score < rounds_to_win:\n"," attack()\n"," randomly()\n"," fight()\n"," winner()\n"," print(\"The current score is: You \", player_score, \" vs CPU \",cpu_score)\n"," played_rounds += 1"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"4rDMKd1RqE-H","colab_type":"text"},"source":["#### 11. Print the winner of the game based on who won more rounds.\n","Remember that the game might be tied. "]},{"cell_type":"code","metadata":{"id":"oBo3alWrqE-J","colab_type":"code","colab":{}},"source":["if player_score > cpu_score:\n"," print('YOU WON THE GAME')\n","elif player_score < cpu_score:\n"," print('YOU LOST THE GAME :(')\n","else:\n"," print('THE GAME IS A TIE')"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"4SQc2w4fqE-P","colab_type":"text"},"source":["# Bonus: Rock, Paper, Scissors, Lizard & Spock\n","![](images/rpsls.jpg)\n","\n","In this challenge, you need to improve the previous game by adding two new options. To know more about the rules of the improved version of rock, paper, scissors, check this [link](http://www.samkass.com/theories/RPSSL.html). \n","\n","In addition, you will also need to improve how the game interacts with the player: the number of rounds to play, which must be an odd number, will be requested to the user until a valid number is entered. Define a new function to make that request.\n","\n","**Hint**: Try to reuse the code that you already coded in the previous challenge. If your code is efficient, this bonus will only consist of simple modifications to the original game."]},{"cell_type":"code","metadata":{"id":"rJgHGV78qE-Q","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]} \ No newline at end of file From 012994e1da433ba696dba92a153ce6501b4e9407 Mon Sep 17 00:00:00 2001 From: Javier Hita <58143227+Javierhvb@users.noreply.github.com> Date: Tue, 3 Mar 2020 11:28:18 +0100 Subject: [PATCH 3/3] Add files via upload --- .../rock-paper-scissors.ipynb" | 465 +++++++++++++++++- 1 file changed, 464 insertions(+), 1 deletion(-) diff --git "a/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" index 27c430b..256ee64 100644 --- "a/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" +++ "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" @@ -1 +1,464 @@ -{"nbformat":4,"nbformat_minor":0,"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.4"},"colab":{"name":"rock-paper-scissors.ipynb","provenance":[]}},"cells":[{"cell_type":"markdown","metadata":{"id":"NS4f3zHKqE83","colab_type":"text"},"source":[""]},{"cell_type":"markdown","metadata":{"id":"qn-RnY8HqE87","colab_type":"text"},"source":["# Rock, Paper & Scissors\n","\n","Let's play the famous game against our computer. You can check the rules [here](https://en.wikipedia.org/wiki/Rock%E2%80%93paper%E2%80%93scissors). \n","\n","## Task\n","Create a program that imitates the playability of the well known game of rock, paper, scissors. Follow the guidelines provided.\n","\n","## Tools\n","1. Loop: **for/while**\n","2. Functions: **input(), print()...**\n","3. Conditional statements: **if, elif, else**\n","4. Definition of functions. Modular programming\n","5. Import modules\n","\n","**To solve this challenge, the use of functions is recommended.**\n","\n","#### 1. Import the choice function of the random module."]},{"cell_type":"code","metadata":{"id":"iqxdmrWtqE89","colab_type":"code","colab":{}},"source":["import random"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"SWW3REbmqE9E","colab_type":"text"},"source":["#### 2. Create a list that includes the 3 possible gesture options of the game: 'rock', 'paper' or 'scissors'. Store the list in a variable called `gestures`."]},{"cell_type":"code","metadata":{"id":"jicJUVkdqE9G","colab_type":"code","colab":{}},"source":["gestures = [\"Rock\", \"Paper\", \"Scissors\"]"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"ZOjduIkvqE9L","colab_type":"text"},"source":["#### 3. Create a variable called `n_rounds` to store the maximum number of rounds to play in a game. \n","Remember that the number of rounds must be odd: 1, 3, 5, ..."]},{"cell_type":"code","metadata":{"id":"2Yrrtl2JqE9M","colab_type":"code","colab":{}},"source":["n_rounds = 5"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"QpxU9BH8qE9R","colab_type":"text"},"source":["#### 4. Create a variable called `rounds_to_win` to store the number of rounds that a player must win to win the game.\n","**Hint**: the value stored in `rounds_to_win` depends on the value of `n_rounds`. "]},{"cell_type":"code","metadata":{"id":"7CRRGVJ0qE9S","colab_type":"code","colab":{}},"source":["rounds_to_win = 3\n","played_rounds = 0"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"S1RLxpxxqE9X","colab_type":"text"},"source":["#### 5. Create two variables to store the number of rounds that the computer and the player have won. Call these variables `cpu_score` and `player_score`."]},{"cell_type":"code","metadata":{"id":"2P4iRpzHqE9Z","colab_type":"code","colab":{}},"source":["cpu_score = 0\n","player_score = 0"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"B7NspPR7qE9e","colab_type":"text"},"source":["#### 6. Define a function that randomly returns one of the 3 gesture options.\n","You will use this function to simulate the gesture choice of the computer. "]},{"cell_type":"code","metadata":{"id":"GKq82i_TqE9g","colab_type":"code","colab":{}},"source":["def randomly():\n"," global random_cpu\n"," random_cpu = random.choice(gestures)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"v7fPsBotqE9n","colab_type":"text"},"source":["#### 7. Define a function that asks the player which is the gesture he or she wants to show: 'rock', 'paper' or 'scissors'.\n","The player should only be allowed to choose one of the 3 gesture options. If the player's choice is not rock, paper or scissors, keep asking until it is."]},{"cell_type":"code","metadata":{"id":"Y45sSj1pqE9p","colab_type":"code","colab":{}},"source":["def attack():\n"," global gesture \n"," gesture = input(\"Choose Rock, Paper or Scissors: \")\n"," while gesture not in gestures:\n"," gesture = input(\"Invalid Input, Choose Rock Paper or Scissors: \")\n"," "],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"epQaP-hYqE9v","colab_type":"text"},"source":["#### 8. Define a function that checks who won a round. \n","The function should return 0 if there is a tie, 1 if the computer wins and 2 if the player wins."]},{"cell_type":"code","metadata":{"id":"6Wpj5-MSqE9x","colab_type":"code","colab":{}},"source":["def fight():\n"," global gesture\n"," global random_cpu\n"," if gesture == random_cpu:\n"," return 0\n"," elif gesture == \"Rock\":\n"," if random_cpu == \"Paper\":\n"," return 1 \n"," else:\n"," return 2\n"," elif gesture == \"Paper\":\n"," if random_cpu == \"Scissors\":\n"," return 1 \n"," else:\n"," return 2\n"," elif gesture == \"Scissors\":\n"," if random_cpu == \"Rock\":\n"," return 1\n"," else:\n"," return 2"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"OFshmt0bqSrd","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":390},"outputId":"8b506e22-63a7-40d3-b59e-30ff7e5e54c5","executionInfo":{"status":"ok","timestamp":1582723356873,"user_tz":-60,"elapsed":6168,"user":{"displayName":"Javier Hita Vallet-Barceló","photoUrl":"","userId":"08244585397579591248"}}},"source":["!pip install tensorflow"],"execution_count":1,"outputs":[{"output_type":"stream","text":["Requirement already satisfied: tensorflow in /usr/local/lib/python3.6/dist-packages (1.15.0)\n","Requirement already satisfied: protobuf>=3.6.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (3.10.0)\n","Requirement already satisfied: wheel>=0.26 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.34.2)\n","Requirement already satisfied: numpy<2.0,>=1.16.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.17.5)\n","Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.1.0)\n","Requirement already satisfied: gast==0.2.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.2.2)\n","Requirement already satisfied: tensorboard<1.16.0,>=1.15.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.15.0)\n","Requirement already satisfied: google-pasta>=0.1.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.1.8)\n","Requirement already satisfied: keras-applications>=1.0.8 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.0.8)\n","Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (3.1.0)\n","Requirement already satisfied: tensorflow-estimator==1.15.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.15.1)\n","Requirement already satisfied: astor>=0.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.8.1)\n","Requirement already satisfied: wrapt>=1.11.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.11.2)\n","Requirement already satisfied: grpcio>=1.8.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.27.1)\n","Requirement already satisfied: absl-py>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.9.0)\n","Requirement already satisfied: keras-preprocessing>=1.0.5 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.1.0)\n","Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.12.0)\n","Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf>=3.6.1->tensorflow) (45.1.0)\n","Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.6/dist-packages (from tensorboard<1.16.0,>=1.15.0->tensorflow) (3.2.1)\n","Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.6/dist-packages (from tensorboard<1.16.0,>=1.15.0->tensorflow) (1.0.0)\n","Requirement already satisfied: h5py in /usr/local/lib/python3.6/dist-packages (from keras-applications>=1.0.8->tensorflow) (2.8.0)\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"AA-2FgWWqE91","colab_type":"text"},"source":["#### 9. Define a function that prints the choice of the computer, the choice of the player and a message that announces who won the current round. \n","You should also use this function to update the variables that count the number of rounds that the computer and the player have won. The score of the winner increases by one point. If there is a tie, the score does not increase."]},{"cell_type":"code","metadata":{"id":"ofiv3JUWqE92","colab_type":"code","colab":{}},"source":["def winner():\n"," print('You chose ' + gesture)\n"," print('Your opponent chose ' + random_cpu)\n"," global cpu_score\n"," global player_score\n"," if fight() == 1:\n"," cpu_score += 1\n"," print(\"You Lose\")\n"," if fight() == 2:\n"," player_score += 1\n"," print(\"You Win!\")\n"," if fight() == 0:\n"," print(\"It's a Tie\")\n"," "],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"7vmR5GZUqE96","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"rLTUICAPqE9_","colab_type":"text"},"source":["#### 10. Now it's time to code the execution of the game using the functions and variables you defined above. \n","\n","First, create a loop structure that repeats while no player reaches the minimum score necessary to win and the number of rounds is less than the maximum number of rounds to play in a game. \n","\n","Inside the loop, use the functions and variables above to create the execution of a round: ask for the player's choice, generate the random choice of the computer, show the round results, update the scores, etc. "]},{"cell_type":"code","metadata":{"id":"HVzvfATnqE-B","colab_type":"code","colab":{}},"source":["while played_rounds < n_rounds and player_score < rounds_to_win and cpu_score < rounds_to_win:\n"," attack()\n"," randomly()\n"," fight()\n"," winner()\n"," print(\"The current score is: You \", player_score, \" vs CPU \",cpu_score)\n"," played_rounds += 1"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"4rDMKd1RqE-H","colab_type":"text"},"source":["#### 11. Print the winner of the game based on who won more rounds.\n","Remember that the game might be tied. "]},{"cell_type":"code","metadata":{"id":"oBo3alWrqE-J","colab_type":"code","colab":{}},"source":["if player_score > cpu_score:\n"," print('YOU WON THE GAME')\n","elif player_score < cpu_score:\n"," print('YOU LOST THE GAME :(')\n","else:\n"," print('THE GAME IS A TIE')"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"4SQc2w4fqE-P","colab_type":"text"},"source":["# Bonus: Rock, Paper, Scissors, Lizard & Spock\n","![](images/rpsls.jpg)\n","\n","In this challenge, you need to improve the previous game by adding two new options. To know more about the rules of the improved version of rock, paper, scissors, check this [link](http://www.samkass.com/theories/RPSSL.html). \n","\n","In addition, you will also need to improve how the game interacts with the player: the number of rounds to play, which must be an odd number, will be requested to the user until a valid number is entered. Define a new function to make that request.\n","\n","**Hint**: Try to reuse the code that you already coded in the previous challenge. If your code is efficient, this bonus will only consist of simple modifications to the original game."]},{"cell_type":"code","metadata":{"id":"rJgHGV78qE-Q","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]} \ No newline at end of file +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "NS4f3zHKqE83" + }, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "qn-RnY8HqE87" + }, + "source": [ + "# Rock, Paper & Scissors\n", + "\n", + "Let's play the famous game against our computer. You can check the rules [here](https://en.wikipedia.org/wiki/Rock%E2%80%93paper%E2%80%93scissors). \n", + "\n", + "## Task\n", + "Create a program that imitates the playability of the well known game of rock, paper, scissors. Follow the guidelines provided.\n", + "\n", + "## Tools\n", + "1. Loop: **for/while**\n", + "2. Functions: **input(), print()...**\n", + "3. Conditional statements: **if, elif, else**\n", + "4. Definition of functions. Modular programming\n", + "5. Import modules\n", + "\n", + "**To solve this challenge, the use of functions is recommended.**\n", + "\n", + "#### 1. Import the choice function of the random module." + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "iqxdmrWtqE89" + }, + "outputs": [], + "source": [ + "import random" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "SWW3REbmqE9E" + }, + "source": [ + "#### 2. Create a list that includes the 3 possible gesture options of the game: 'rock', 'paper' or 'scissors'. Store the list in a variable called `gestures`." + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "jicJUVkdqE9G" + }, + "outputs": [], + "source": [ + "gestures = [\"Rock\", \"Paper\", \"Scissors\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "ZOjduIkvqE9L" + }, + "source": [ + "#### 3. Create a variable called `n_rounds` to store the maximum number of rounds to play in a game. \n", + "Remember that the number of rounds must be odd: 1, 3, 5, ..." + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "2Yrrtl2JqE9M" + }, + "outputs": [], + "source": [ + "n_rounds = 5" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "QpxU9BH8qE9R" + }, + "source": [ + "#### 4. Create a variable called `rounds_to_win` to store the number of rounds that a player must win to win the game.\n", + "**Hint**: the value stored in `rounds_to_win` depends on the value of `n_rounds`. " + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "7CRRGVJ0qE9S" + }, + "outputs": [], + "source": [ + "rounds_to_win = 3\n", + "played_rounds = 0" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "S1RLxpxxqE9X" + }, + "source": [ + "#### 5. Create two variables to store the number of rounds that the computer and the player have won. Call these variables `cpu_score` and `player_score`." + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "2P4iRpzHqE9Z" + }, + "outputs": [], + "source": [ + "cpu_score = 0\n", + "player_score = 0" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "B7NspPR7qE9e" + }, + "source": [ + "#### 6. Define a function that randomly returns one of the 3 gesture options.\n", + "You will use this function to simulate the gesture choice of the computer. " + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "GKq82i_TqE9g" + }, + "outputs": [], + "source": [ + "def randomly():\n", + " global random_cpu\n", + " random_cpu = random.choice(gestures)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "v7fPsBotqE9n" + }, + "source": [ + "#### 7. Define a function that asks the player which is the gesture he or she wants to show: 'rock', 'paper' or 'scissors'.\n", + "The player should only be allowed to choose one of the 3 gesture options. If the player's choice is not rock, paper or scissors, keep asking until it is." + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "Y45sSj1pqE9p" + }, + "outputs": [], + "source": [ + "def attack():\n", + " global gesture \n", + " gesture = input(\"Choose Rock, Paper or Scissors: \")\n", + " while gesture not in gestures:\n", + " gesture = input(\"Invalid Input, Choose Rock Paper or Scissors: \")\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "epQaP-hYqE9v" + }, + "source": [ + "#### 8. Define a function that checks who won a round. \n", + "The function should return 0 if there is a tie, 1 if the computer wins and 2 if the player wins." + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "6Wpj5-MSqE9x" + }, + "outputs": [], + "source": [ + "def fight():\n", + " global gesture\n", + " global random_cpu\n", + " if gesture == random_cpu:\n", + " return 0\n", + " elif gesture == \"Rock\":\n", + " if random_cpu == \"Paper\":\n", + " return 1 \n", + " else:\n", + " return 2\n", + " elif gesture == \"Paper\":\n", + " if random_cpu == \"Scissors\":\n", + " return 1 \n", + " else:\n", + " return 2\n", + " elif gesture == \"Scissors\":\n", + " if random_cpu == \"Rock\":\n", + " return 1\n", + " else:\n", + " return 2" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 390 + }, + "colab_type": "code", + "executionInfo": { + "elapsed": 6168, + "status": "ok", + "timestamp": 1582723356873, + "user": { + "displayName": "Javier Hita Vallet-Barceló", + "photoUrl": "", + "userId": "08244585397579591248" + }, + "user_tz": -60 + }, + "id": "OFshmt0bqSrd", + "outputId": "8b506e22-63a7-40d3-b59e-30ff7e5e54c5" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: tensorflow in /usr/local/lib/python3.6/dist-packages (1.15.0)\n", + "Requirement already satisfied: protobuf>=3.6.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (3.10.0)\n", + "Requirement already satisfied: wheel>=0.26 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.34.2)\n", + "Requirement already satisfied: numpy<2.0,>=1.16.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.17.5)\n", + "Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.1.0)\n", + "Requirement already satisfied: gast==0.2.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.2.2)\n", + "Requirement already satisfied: tensorboard<1.16.0,>=1.15.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.15.0)\n", + "Requirement already satisfied: google-pasta>=0.1.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.1.8)\n", + "Requirement already satisfied: keras-applications>=1.0.8 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.0.8)\n", + "Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (3.1.0)\n", + "Requirement already satisfied: tensorflow-estimator==1.15.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.15.1)\n", + "Requirement already satisfied: astor>=0.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.8.1)\n", + "Requirement already satisfied: wrapt>=1.11.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.11.2)\n", + "Requirement already satisfied: grpcio>=1.8.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.27.1)\n", + "Requirement already satisfied: absl-py>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (0.9.0)\n", + "Requirement already satisfied: keras-preprocessing>=1.0.5 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.1.0)\n", + "Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow) (1.12.0)\n", + "Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf>=3.6.1->tensorflow) (45.1.0)\n", + "Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.6/dist-packages (from tensorboard<1.16.0,>=1.15.0->tensorflow) (3.2.1)\n", + "Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.6/dist-packages (from tensorboard<1.16.0,>=1.15.0->tensorflow) (1.0.0)\n", + "Requirement already satisfied: h5py in /usr/local/lib/python3.6/dist-packages (from keras-applications>=1.0.8->tensorflow) (2.8.0)\n" + ] + } + ], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "AA-2FgWWqE91" + }, + "source": [ + "#### 9. Define a function that prints the choice of the computer, the choice of the player and a message that announces who won the current round. \n", + "You should also use this function to update the variables that count the number of rounds that the computer and the player have won. The score of the winner increases by one point. If there is a tie, the score does not increase." + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "ofiv3JUWqE92" + }, + "outputs": [], + "source": [ + "def winner():\n", + " print('You chose ' + gesture)\n", + " print('Your opponent chose ' + random_cpu)\n", + " global cpu_score\n", + " global player_score\n", + " if fight() == 1:\n", + " cpu_score += 1\n", + " print(\"You Lose\")\n", + " if fight() == 2:\n", + " player_score += 1\n", + " print(\"You Win!\")\n", + " if fight() == 0:\n", + " print(\"It's a Tie\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "7vmR5GZUqE96" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "rLTUICAPqE9_" + }, + "source": [ + "#### 10. Now it's time to code the execution of the game using the functions and variables you defined above. \n", + "\n", + "First, create a loop structure that repeats while no player reaches the minimum score necessary to win and the number of rounds is less than the maximum number of rounds to play in a game. \n", + "\n", + "Inside the loop, use the functions and variables above to create the execution of a round: ask for the player's choice, generate the random choice of the computer, show the round results, update the scores, etc. " + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "HVzvfATnqE-B" + }, + "outputs": [], + "source": [ + "while played_rounds < n_rounds and player_score < rounds_to_win and cpu_score < rounds_to_win:\n", + " attack()\n", + " randomly()\n", + " fight()\n", + " winner()\n", + " print(\"The current score is: You \", player_score, \" vs CPU \",cpu_score)\n", + " played_rounds += 1" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "4rDMKd1RqE-H" + }, + "source": [ + "#### 11. Print the winner of the game based on who won more rounds.\n", + "Remember that the game might be tied. " + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "oBo3alWrqE-J" + }, + "outputs": [], + "source": [ + "if player_score > cpu_score:\n", + " print('YOU WON THE GAME')\n", + "elif player_score < cpu_score:\n", + " print('YOU LOST THE GAME :(')\n", + "else:\n", + " print('THE GAME IS A TIE')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "4SQc2w4fqE-P" + }, + "source": [ + "# Bonus: Rock, Paper, Scissors, Lizard & Spock\n", + "![](images/rpsls.jpg)\n", + "\n", + "In this challenge, you need to improve the previous game by adding two new options. To know more about the rules of the improved version of rock, paper, scissors, check this [link](http://www.samkass.com/theories/RPSSL.html). \n", + "\n", + "In addition, you will also need to improve how the game interacts with the player: the number of rounds to play, which must be an odd number, will be requested to the user until a valid number is entered. Define a new function to make that request.\n", + "\n", + "**Hint**: Try to reuse the code that you already coded in the previous challenge. If your code is efficient, this bonus will only consist of simple modifications to the original game." + ] + }, + { + "cell_type": "code", + "execution_count": 0, + "metadata": { + "colab": {}, + "colab_type": "code", + "id": "rJgHGV78qE-Q" + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "name": "rock-paper-scissors.ipynb", + "provenance": [] + }, + "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.4" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +}