From 4def7605b1cc5bd07e420d8642c4b15065524fd7 Mon Sep 17 00:00:00 2001 From: Sofia Sousa Date: Mon, 5 Aug 2019 12:24:01 +0100 Subject: [PATCH 1/8] 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 159d4174a87c5c46220f957032edb74c0e37a1de Mon Sep 17 00:00:00 2001 From: Sofia Sousa Date: Wed, 7 Aug 2019 17:27:06 +0100 Subject: [PATCH 2/8] half-way --- bus/bus.ipynb | 66 ++++++++- duel/duel.ipynb | 139 ++++++++++++++++-- robin-hood/robin-hood.ipynb | 72 ++++++++- .../rock-paper-scissors.ipynb" | 13 +- 4 files changed, 263 insertions(+), 27 deletions(-) diff --git a/bus/bus.ipynb b/bus/bus.ipynb index 81779ce..6ada743 100644 --- a/bus/bus.ipynb +++ b/bus/bus.ipynb @@ -37,16 +37,34 @@ "outputs": [], "source": [ "# variables\n", - "\n" + "p_in = [5,9,2,4,3]\n", + "p_out = [0,3,5,2,5]\n", + "\n", + "stops = []\n", + "for i in range (0,len(p_in)):\n", + " stop=(p_in[i], p_out[i])\n", + " stops.append(stop)\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# 1. Calculate the number of stops.\n", + "len(stops)\n", "\n" ] }, @@ -58,27 +76,59 @@ "source": [ "# 2. Assign a variable a list whose elements are the number of passengers in each stop: \n", "# Each item depends on the previous item in the list + in - out.\n", - "\n" + "stocking = []\n", + "passengers = 0\n", + "for stop in stops:\n", + " passengers = passengers + stop[0] - stop[1]\n", + " stocking.append(passengers)\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# 3. Find the maximum occupation of the bus.\n", - "\n" + "max(stocking)\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8.4\n", + "2.0591260281974\n" + ] + } + ], "source": [ "# 4. Calculate the average occupation. And the standard deviation.\n", - "\n" + "average_occupation = sum(stocking)/len(stocking)\n", + "print (average_occupation)\n", + "\n", + "squared_deviations=[]\n", + "for i in stocking:\n", + " squared_deviation=(i - average_occupation)**2\n", + " squared_deviations.append(squared_deviation)\n", + "standard_deviation = (sum(squared_deviations)/len(squared_deviations))**0.5\n", + "print(standard_deviation)\n" ] }, { @@ -105,7 +155,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.4" } }, "nbformat": 4, diff --git a/duel/duel.ipynb b/duel/duel.ipynb index 4398d88..4317ba8 100644 --- a/duel/duel.ipynb +++ b/duel/duel.ipynb @@ -49,7 +49,9 @@ "metadata": {}, "outputs": [], "source": [ - "# Assign 0 to each variable that stores the victories\n" + "# Assign 0 to each variable that stores the victories\n", + "gandalf_victories=0\n", + "saruman_victories=0" ] }, { @@ -58,18 +60,41 @@ "metadata": {}, "outputs": [], "source": [ - "# Execution of spell clashes\n" + "# Execution of spell clashes\n", + "spell_clashes=[]\n", + "for i in range(0,len(gandalf)):\n", + " clash_result = gandalf[i] - saruman[i]\n", + " spell_clashes.append(clash_result)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf wins\n" + ] + } + ], "source": [ "# We check who has won, do not forget the possibility of a draw.\n", "# Print the result based on the winner.\n", - "\n" + "for i in spell_clashes:\n", + " if i < 0:\n", + " saruman_victories = saruman_victories + 1\n", + " elif i > 0:\n", + " gandalf_victories = gandalf_victories + 1\n", + "\n", + "if saruman_victories > gandalf_victories:\n", + " print(\"Saruman wins\")\n", + "elif saruman_victories < gandalf_victories:\n", + " print(\"Gandalf wins\")\n", + "else:\n", + " print(\"Tie\")\n" ] }, { @@ -144,24 +169,66 @@ "outputs": [], "source": [ "# Assign spell power lists to variables\n", - "\n" + "gandalf_power = []\n", + "saruman_power = []\n", + "\n", + "for i in gandalf:\n", + " power = POWER[i]\n", + " gandalf_power.append(power)\n", + " \n", + "for i in saruman:\n", + " power = POWER[i]\n", + " saruman_power.append(power)\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf Wins\n" + ] + } + ], "source": [ "# 2. A sorcerer wins if he succeeds in winning 3 spell clashes in a row.\n", "\n", "\n", "# Execution of spell clashes\n", - "\n", + "spell_clashes=[]\n", + "for i in range(0,len(gandalf_power)):\n", + " clash_result = gandalf_power[i] - saruman_power[i]\n", + " spell_clashes.append(clash_result)\n", "\n", "\n", "# check for 3 wins in a row\n", + "saruman_results = []\n", + "gandalf_results = []\n", + "for i in spell_clashes:\n", + " if i < 0:\n", + " saruman_results.append(1)\n", + " gandalf_results.append(0)\n", + " elif i > 0:\n", + " gandalf_results.append(1)\n", + " saruman_results.append(0)\n", "\n", + "gandalf_pontuation = 0\n", + "saruman_pontuation = 0\n", + "for i in gandalf_results:\n", + " if i == 1:\n", + " gandalf_pontuation = gandalf_pontuation + 1\n", + " saruman_pontuation = 0\n", + " if gandalf_pontuation == 3:\n", + " print(\"Gandalf Wins\")\n", + " elif i == 0:\n", + " gandalf_pontuation = 0\n", + " saruman_pontuation = saruman_pontuation + 1\n", + " if saruman_pontuation == 3:\n", + " print(\"Saruman Wins\")\n", "\n", "# check the winner\n" ] @@ -170,22 +237,70 @@ "cell_type": "code", "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "35.0\n", + "30.5\n" + ] + } + ], "source": [ "# 3. Average of each of the spell lists.\n", - "\n" + "average_gandalf_power=sum(gandalf_power)/len(gandalf_power)\n", + "print(average_gandalf_power)\n", + "average_saruman_power=sum(saruman_power)/len(saruman_power)\n", + "print(average_saruman_power)\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "16.881943016134134\n", + "15.56438241627338\n" + ] + } + ], "source": [ "# 4. Standard deviation of each of the spell lists.\n", - "\n" + "\n", + "squared_deviations_gandalf=[]\n", + "for i in gandalf_power:\n", + " squared_deviation=(i - average_gandalf_power)**2\n", + " squared_deviations_gandalf.append(squared_deviation)\n", + "standard_deviation_gandalf = (sum(squared_deviations_gandalf)/len(squared_deviations_gandalf))**0.5\n", + "print(standard_deviation_gandalf)\n", + "\n", + "squared_deviations_saruman=[]\n", + "for i in saruman_power:\n", + " squared_deviation=(i - average_saruman_power)**2\n", + " squared_deviations_saruman.append(squared_deviation)\n", + "standard_deviation_saruman = (sum(squared_deviations_saruman)/len(squared_deviations_saruman))**0.5\n", + "print(standard_deviation_saruman)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -210,7 +325,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.7" + "version": "3.7.4" } }, "nbformat": 4, diff --git a/robin-hood/robin-hood.ipynb b/robin-hood/robin-hood.ipynb index b1af06b..0c7b65e 100644 --- a/robin-hood/robin-hood.ipynb +++ b/robin-hood/robin-hood.ipynb @@ -69,9 +69,43 @@ "cell_type": "code", "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "6\n", + "2\n", + "2\n", + "2\n" + ] + } + ], "source": [ "# 2. Calculate how many arrows have fallen in each quadrant.\n", + "quadrant1 = 0\n", + "quadrant2 = 0\n", + "quadrant3 = 0\n", + "quadrant4 = 0\n", + "between_quadrants=0\n", + "for i in points:\n", + " if (i[0] > 0) and (i[1] > 0):\n", + " quadrant1 = quadrant1 + 1\n", + " elif (i[0] < 0) and (i[1] > 0):\n", + " quadrant2 = quadrant2 + 1\n", + " elif (i[0] < 0) and (i[1] < 0):\n", + " quadrant3 = quadrant3 + 1\n", + " elif (i[0]>0) and (i[1]<0):\n", + " quadrant4 = quadrant4 + 1\n", + " elif (i[0]==0) or (i[1]==0):\n", + " between_quadrants=between_quadrants+1\n", + "print(quadrant1)\n", + "print(quadrant2)\n", + "print(quadrant3)\n", + "print(quadrant4)\n", + "print(between_quadrants)\n", + "\n", "\n" ] }, @@ -79,10 +113,25 @@ "cell_type": "code", "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(0, 2)\n" + ] + } + ], "source": [ "# 3. Find the point closest to the center. Calculate its distance to the center\n", "# Defining a function that calculates the distance to the center can help.\n", + "distances=[]\n", + "for i in points:\n", + " distance = ((i[0]** 2) + (i[1]** 2))** 0.5\n", + " distances.append(distance)\n", + " \n", + "print(points[distances.index(min(distances))])\n", + "\n", "\n" ] }, @@ -90,11 +139,24 @@ "cell_type": "code", "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "# 4. If the target has a radius of 9, calculate the number of arrows that \n", "# must be picked up in the forest.\n", - "\n" + "\n", + "outside_arrows=0\n", + "for i in distances:\n", + " if i > 9:\n", + " outside_arrows=outside_arrows+1\n", + "print(outside_arrows)" ] }, { @@ -121,7 +183,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.4" } }, "nbformat": 4, diff --git "a/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" "b/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" index f13735d..837f80c 100644 --- "a/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" +++ "b/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" @@ -31,17 +31,26 @@ "source": [ "# Import the choice function of the random module\n", "# https://stackoverflow.com/questions/306400/how-to-randomly-select-an-item-from-a-list\n", + "import random\n", + "\n", "\n", "# Assign to a list the 3 possible options: 'stone', 'paper' or 'scissors'.\n", + "game=['stone','paper','scissors']\n", + "\n", "\n", "# Assign a variable to the maximum number of games: 1, 3, 5, etc ...\n", + "maximum_games=10\n", + "\n", "\n", "# Assign a variable to the number of games a player must win to win.\n", "# Preferably the value will be based on the number of maximum games\n", + "games_win=6\n", + "\n", "\n", "# Define a function that randomly returns one of the 3 options.\n", "# This will correspond to the play of the machine. Totally random.\n", - "\n", + "def machine_play():\n", + " print(random.choice(game))\n", "\n", "# Define a function that asks your choice: 'stone', 'paper' or 'scissors'\n", "# you should only allow one of the 3 options. This is defensive programming.\n", @@ -171,7 +180,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.7" + "version": "3.7.4" } }, "nbformat": 4, From 253ab197cbb7dea90d3122078a4fc544fbe167de Mon Sep 17 00:00:00 2001 From: Sofia Sousa Date: Wed, 7 Aug 2019 17:49:33 +0100 Subject: [PATCH 3/8] 2 --- bus/bus.ipynb | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/bus/bus.ipynb b/bus/bus.ipynb index 6ada743..cc561b9 100644 --- a/bus/bus.ipynb +++ b/bus/bus.ipynb @@ -32,32 +32,26 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# variables\n", - "p_in = [5,9,2,4,3]\n", - "p_out = [0,3,5,2,5]\n", - "\n", - "stops = []\n", - "for i in range (0,len(p_in)):\n", - " stop=(p_in[i], p_out[i])\n", - " stops.append(stop)\n" + "stops = [(10, 0), (4, 1), (3, 5), (3, 4), (5, 1), (1, 5), (5, 8 ), (4, 6), (2, 3)]" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "5" + "9" ] }, - "execution_count": 2, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -70,7 +64,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -85,16 +79,16 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "11" + "14" ] }, - "execution_count": 4, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -106,15 +100,15 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "8.4\n", - "2.0591260281974\n" + "9.333333333333334\n", + "3.197221015541813\n" ] } ], @@ -131,6 +125,13 @@ "print(standard_deviation)\n" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, From 293733aff6978b29cc456ae7717696a17a8db0b9 Mon Sep 17 00:00:00 2001 From: Sofia Sousa Date: Thu, 8 Aug 2019 09:39:37 +0100 Subject: [PATCH 4/8] first part of rock-paper-scissors --- duel/duel.ipynb | 4 +- robin-hood/robin-hood.ipynb | 2 +- .../rock-paper-scissors.ipynb" | 58 ++++++++++++++++--- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/duel/duel.ipynb b/duel/duel.ipynb index 4317ba8..4ab7ad7 100644 --- a/duel/duel.ipynb +++ b/duel/duel.ipynb @@ -85,9 +85,9 @@ "# Print the result based on the winner.\n", "for i in spell_clashes:\n", " if i < 0:\n", - " saruman_victories = saruman_victories + 1\n", + " saruman_victories += 1\n", " elif i > 0:\n", - " gandalf_victories = gandalf_victories + 1\n", + " gandalf_victories += 1\n", "\n", "if saruman_victories > gandalf_victories:\n", " print(\"Saruman wins\")\n", diff --git a/robin-hood/robin-hood.ipynb b/robin-hood/robin-hood.ipynb index 0c7b65e..9475455 100644 --- a/robin-hood/robin-hood.ipynb +++ b/robin-hood/robin-hood.ipynb @@ -155,7 +155,7 @@ "outside_arrows=0\n", "for i in distances:\n", " if i > 9:\n", - " outside_arrows=outside_arrows+1\n", + " outside_arrows += 1\n", "print(outside_arrows)" ] }, diff --git "a/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" "b/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" index 837f80c..b727cd1 100644 --- "a/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" +++ "b/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" @@ -39,31 +39,62 @@ "\n", "\n", "# Assign a variable to the maximum number of games: 1, 3, 5, etc ...\n", - "maximum_games=10\n", + "maximum_games=5\n", "\n", "\n", "# Assign a variable to the number of games a player must win to win.\n", "# Preferably the value will be based on the number of maximum games\n", - "games_win=6\n", + "games_win=int(maximum_games/2)+1\n", "\n", "\n", "# Define a function that randomly returns one of the 3 options.\n", "# This will correspond to the play of the machine. Totally random.\n", "def machine_play():\n", - " print(random.choice(game))\n", + " return random.choice(game)\n", "\n", "# Define a function that asks your choice: 'stone', 'paper' or 'scissors'\n", "# you should only allow one of the 3 options. This is defensive programming.\n", "# If it is not stone, paper or scissors keep asking until it is.\n", - "\n", + "def ask_choice():\n", + " user_choice=input(\"What is your play?\")\n", + " while (user_choice != game[0]) and (user_choice != game[1]) and (user_choice != game[2]):\n", + " user_choice = input(\"The answer is not valid, choose one of the following: scissors, stone or paper:\")\n", + " print (\"ok\")\n", + " return user_choice\n", "\n", "# Define a function that resolves a combat.\n", "# Returns 0 if there is a tie, 1 if the machine wins, 2 if the human player wins\n", - "\n", - " \n", + "machine=0\n", + "user=0\n", + "\n", + "def combat():\n", + " global machine\n", + " machine = machine_play()\n", + " global user\n", + " user = ask_choice()\n", + " if ((user == game[0]) and (machine == game[2])) or ((user == game[1]) and (machine == game[0])) or (\n", + " (user == game[2]) and (machine == game[1])):\n", + " return (2)\n", + " elif ((user == game[0]) and (machine == game[0])) or ((user == game[1]) and (machine == game[1])) or (\n", + " (user == game[2]) and (machine == game[2])):\n", + " return (0)\n", + " else:\n", + " return (1)\n", + " \n", "# Define a function that shows the choice of each player and the state of the game\n", "# This function should be used every time accumulated points are updated\n", - "\n", + "user_pontuation=0\n", + "machine_pontuation=0\n", + "\n", + "def game_play ():\n", + " play_result=combat()\n", + " if play_result==2:\n", + " global user_pontuation\n", + " user_pontuation +=1\n", + " if play_result==1:\n", + " global machine_pontuation\n", + " machine_pontuation +=1\n", + " print(\"User played\", user, \"and machine played\", machine, \". Total pontuation is\", user_pontuation, \" for user, and\", machine_pontuation, \"for machine\")\n", " \n", "# Create two variables that accumulate the wins of each participant\n", "\n", @@ -73,10 +104,19 @@ "# machine and ask the player's. Compare them and update the value of the variables\n", "# that accumulate the wins of each participant.\n", "\n", - "\n", + "def full_game():\n", + " global machine_pontuation\n", + " global user_pontuation\n", + " while (machine_pontuation Date: Thu, 8 Aug 2019 09:41:37 +0100 Subject: [PATCH 5/8] first part of rock-paper-scissors --- .../rock-paper-scissors.ipynb" | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git "a/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" "b/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" index b727cd1..f2f9a2a 100644 --- "a/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" +++ "b/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" @@ -27,7 +27,16 @@ "cell_type": "code", "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (, line 88)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m88\u001b[0m\n\u001b[0;31m def winner()\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], "source": [ "# Import the choice function of the random module\n", "# https://stackoverflow.com/questions/306400/how-to-randomly-select-an-item-from-a-list\n", @@ -115,8 +124,7 @@ " print (\"User has won the game.\")\n", " \n", " \n", - "# Print by console the winner of the game based on who has more accumulated wins\n", - "def winner()" + "# Print by console the winner of the game based on who has more accumulated wins" ] }, { From f61af213bdfebc87df1ef76a4720345b2872ef85 Mon Sep 17 00:00:00 2001 From: Sofia Sousa Date: Thu, 8 Aug 2019 11:27:11 +0100 Subject: [PATCH 6/8] snail and well --- .../rock-paper-scissors.ipynb" | 78 ++++++++++++++----- snail-and-well/snail-and-well.ipynb | 62 ++++++++++++--- 2 files changed, 109 insertions(+), 31 deletions(-) diff --git "a/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" "b/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" index f2f9a2a..fec5bf7 100644 --- "a/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" +++ "b/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" @@ -25,18 +25,9 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "invalid syntax (, line 88)", - "output_type": "error", - "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m88\u001b[0m\n\u001b[0;31m def winner()\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" - ] - } - ], + "outputs": [], "source": [ "# Import the choice function of the random module\n", "# https://stackoverflow.com/questions/306400/how-to-randomly-select-an-item-from-a-list\n", @@ -155,49 +146,96 @@ "outputs": [], "source": [ "# Import the choice function of the random module\n", - "\n", + "import random\n", "\n", "# Define a function that asks for an odd number on the keyboard, until it is not valid\n", "# will keep asking\n", - "\n", + "def odd_number():\n", + " odd_number= int (input(\"Insert and odd number:\"))\n", + " while odd_number % 2 != 0:\n", + " odd_number=int (input (\"That is an even number, please insert an odd number:\"))\n", + " return odd_number\n", "\n", "# Assign a list of 5 possible options.\n", "\n", + "game = ['stone', 'paper', 'scissors', 'lizard', 'spock']\n", + "\n", "\n", "# Assign a variable to the maximum number of games: 1, 3, 5, etc ...\n", "# This time the previously defined function is used\n", - "\n", + "maximum_games=5\n", "\n", "# Assign a variable to the number of games a player must win to win.\n", "# Preferably the value will be based on the number of maximum games\n", - "\n", + "games_win=int(maximum_games/2)+1\n", "\n", "# Define a function that randomly returns one of the 5 options.\n", "# This will correspond to the play of the machine. Totally random.\n", - "\n", + "def machine_play():\n", + " return random.choice(game)\n", "\n", "# Define a function that asks your choice between 5\n", "# you should only allow one of the 5 options. This is defensive programming.\n", "# If it is not valid, keep asking until it is valid.\n", - "\n", + "def ask_choice():\n", + " user_choice=input(\"What is your play?\")\n", + " while (user_choice != game[0]) and (user_choice != game[1]) and (user_choice != game[2]) and (user_choice != game[3]) and (user_choice != game[4]):\n", + " user_choice = input(\"The answer is not valid, choose one of the following: scissors, stone or paper:\")\n", + " print (\"ok\")\n", + " return user_choice\n", "\n", "# Define a function that resolves a combat.\n", "# Returns 0 if there is a tie, 1 if the machine wins, 2 if the human player wins\n", "# Now there are more options\n", - " \n", - "\n", + "def combat():\n", + " global machine\n", + " machine = machine_play()\n", + " global user\n", + " user = ask_choice()\n", + " if ((user == game[0]) and ((machine == game[2]) or (machine == game[3]))) or (\n", + " (user == game[1]) and ((machine == game[0]) or (machine == game[4]))) or (\n", + " (user == game[2]) and ((machine == game[1]) or (machine == game[3]))) or (\n", + " (user == game[3]) and ((machine == game[4]) or (machine == game[1]))) or (\n", + " (user == game[4]) and ((machine == game[2]) or (machine == game[0]))):\n", + " return (2)\n", + " elif ((user == game[0]) and (machine == game[0])) or ((user == game[1]) and (machine == game[1])) or (\n", + " (user == game[2]) and (machine == game[2])) or ((user == game[3]) and (machine == game[3])) or ((user == game[4]) and (machine == game[4])):\n", + " return (0)\n", + " else:\n", + " return (1)\n", " \n", "# Define a function that shows the choice of each player and the state of the game\n", "# This function should be used every time accumulated points are updated\n", + "user_pontuation=0\n", + "machine_pontuation=0\n", "\n", + "def game_play ():\n", + " play_result=combat()\n", + " if play_result==2:\n", + " global user_pontuation\n", + " user_pontuation +=1\n", + " if play_result==1:\n", + " global machine_pontuation\n", + " machine_pontuation +=1\n", + " print(\"User played\", user, \"and machine played\", machine, \". Total pontuation is\", user_pontuation, \" for user, and\", machine_pontuation, \"for machine\")\n", " \n", + "\n", "# Create two variables that accumulate the wins of each participant\n", "\n", "# Create a loop that iterates while no player reaches the minimum of wins\n", "# necessary to win. Inside the loop solves the play of the\n", "# machine and ask the player's. Compare them and update the value of the variables\n", "# that accumulate the wins of each participant.\n", - "\n", + "def full_game():\n", + " global machine_pontuation\n", + " global user_pontuation\n", + " while (machine_pontuation