Break into parts

This commit is contained in:
Jimmy 2021-12-14 00:51:51 +13:00
parent 402a92cf25
commit 40c080ae51
1 changed files with 327 additions and 296 deletions

View File

@ -10,93 +10,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 25, "execution_count": null,
"id": "d8f95dd9-11ad-455d-854a-86a12d84fbda", "id": "d8f95dd9-11ad-455d-854a-86a12d84fbda",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a three digit number: 123\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3 \n",
" \n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a three digit number: 456\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"4 5 6 \n",
" P \n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a three digit number: 789\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"7 8 9 \n",
" F \n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a three digit number: 588\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"5 8 8 \n",
"F F P \n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a three digit number: 585\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"5 8 5 \n",
"F F P \n",
"\n",
"You loose!\n",
"5 8 0 "
]
}
],
"source": [ "source": [
"%run picofumi.py" "%run picofumi.py"
] ]
@ -111,18 +28,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": null,
"id": "0046af84-de64-4023-bdf5-335177caf22a", "id": "0046af84-de64-4023-bdf5-335177caf22a",
"metadata": {}, "metadata": {
"outputs": [ "tags": []
{ },
"name": "stdout", "outputs": [],
"output_type": "stream",
"text": [
"Pico Fumi\n"
]
}
],
"source": [ "source": [
"print(\"Pico Fumi\")" "print(\"Pico Fumi\")"
] ]
@ -137,38 +48,24 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": null,
"id": "781cbd75-5b16-4740-ac88-fda9672bcdbd", "id": "781cbd75-5b16-4740-ac88-fda9672bcdbd",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"123\n"
]
}
],
"source": [ "source": [
"#string\n",
"guess = \"123\"\n", "guess = \"123\"\n",
"print(guess)" "print(guess)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": null,
"id": "b41186fa-ff8a-4e5e-ba55-1c9450a5c9af", "id": "b41186fa-ff8a-4e5e-ba55-1c9450a5c9af",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
}
],
"source": [ "source": [
"#integer \n",
"guesses = 0\n", "guesses = 0\n",
"print(guesses)" "print(guesses)"
] ]
@ -183,9 +80,122 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 16,
"id": "3adeee26-8a18-4bad-946b-3d06920ef82f", "id": "3adeee26-8a18-4bad-946b-3d06920ef82f",
"metadata": {}, "metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a three digit number: 1\n"
]
}
],
"source": [
"guess = input(\"Enter a three digit number:\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "ec055f06-6fdd-4ab9-bce2-d56c82093c4b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"print(guess)"
]
},
{
"cell_type": "markdown",
"id": "8999ef19-6a3c-41ea-bf2b-11dcbbe64070",
"metadata": {},
"source": [
"## Check that the guess a 3 digit number"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "f7bcf18f-4e98-453f-bc1f-ef61b0aa55e4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(guess)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "53fca278-f55e-43f1-ab5f-f6df4fb7c163",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(guess) == 3"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "bd9b231e-53c5-4b05-8f55-86ccf289b1e1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Guess is not 3 digits\n"
]
}
],
"source": [
"if len(guess) != 3:\n",
" print(\"Guess is not 3 digits\")"
]
},
{
"cell_type": "markdown",
"id": "2f806636-3296-42ce-914f-356e34e066a7",
"metadata": {},
"source": [
"## Re-prompt the user until they enter a 3 digit number"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "d427210a-75be-4a47-83e2-fbdffa2b2644",
"metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdin", "name": "stdin",
@ -196,25 +206,9 @@
} }
], ],
"source": [ "source": [
"guess = input(\"Enter a three digit number:\")" "guess = input(\"Enter a three digit number:\")\n",
] "while len(guess) != 3:\n",
}, " guess = input(\"Enter a three digit number:\")"
{
"cell_type": "code",
"execution_count": 16,
"id": "ec055f06-6fdd-4ab9-bce2-d56c82093c4b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123\n"
]
}
],
"source": [
"print(guess)"
] ]
}, },
{ {
@ -227,7 +221,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 56,
"id": "7e1a2228-3316-4652-9051-70c355de94bb", "id": "7e1a2228-3316-4652-9051-70c355de94bb",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -235,13 +229,17 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"['1', '2', '3']\n" "['1', '2', '3']\n",
"1\n",
"3\n"
] ]
} }
], ],
"source": [ "source": [
"guess = list(guess)\n", "guess = list(guess)\n",
"print(guess)" "print(guess)\n",
"print(guess[0])\n",
"print(guess[2])"
] ]
}, },
{ {
@ -249,12 +247,12 @@
"id": "ccd7b9d1-c266-498e-9f46-63c522c4d332", "id": "ccd7b9d1-c266-498e-9f46-63c522c4d332",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Itterate over the parts" "## Itterate over the guess"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 26, "execution_count": 57,
"id": "ddca84c9-2fff-4302-afd0-7096649bcdc7", "id": "ddca84c9-2fff-4302-afd0-7096649bcdc7",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -262,9 +260,9 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"5\n", "1\n",
"8\n", "2\n",
"5\n" "3\n"
] ]
} }
], ],
@ -273,6 +271,80 @@
" print(part)" " print(part)"
] ]
}, },
{
"cell_type": "code",
"execution_count": 58,
"id": "f631cc60-92e9-4c16-a5ab-4f50dfd10cc2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"index 0 value 1\n",
"index 1 value 2\n",
"index 2 value 3\n"
]
}
],
"source": [
"for index, value in enumerate(guess):\n",
" print(f'index {index} value {value}')"
]
},
{
"cell_type": "markdown",
"id": "201cae7a-2cc5-4ff0-ac16-730091e24b72",
"metadata": {},
"source": [
"## Functions"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "3aebea62-fa40-4e87-8229-3aafb051a006",
"metadata": {},
"outputs": [],
"source": [
"def GetGuess():\n",
" guess = input(\"Enter a three digit number:\")\n",
" while len(guess) != 3:\n",
" guess = input(\"Enter a three digit number:\")\n",
" return list(guess)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "a2430233-5753-433d-bada-d8ee086f2bb2",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a three digit number: 12\n",
"Enter a three digit number: 12\n",
"Enter a three digit number: 34\n",
"Enter a three digit number: 123\n"
]
},
{
"data": {
"text/plain": [
"['1', '2', '3']"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"GetGuess()"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "77ab1ca2-0ea0-43b3-b6bd-d4438fa8141d", "id": "77ab1ca2-0ea0-43b3-b6bd-d4438fa8141d",
@ -283,7 +355,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 27, "execution_count": 35,
"id": "f4412eef-6b79-4329-b8e7-85f916d3dfbf", "id": "f4412eef-6b79-4329-b8e7-85f916d3dfbf",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -294,10 +366,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 44,
"id": "c9324728-63c2-4023-92fc-3e1e80dff03a", "id": "c9324728-63c2-4023-92fc-3e1e80dff03a",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"536\n"
]
}
],
"source": [ "source": [
"number = random.randrange(100,999)\n", "number = random.randrange(100,999)\n",
"print(number)" "print(number)"
@ -305,95 +385,55 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 47,
"id": "6194de2b-2dbe-4c49-bc00-e5bed3c35f1f", "id": "183e5444-2b79-4366-84cf-4da91c2547cf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(number)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "afe9d668-aefd-4021-9028-a21b04e931cd",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"finished = False" "number = str(number)\n",
"number = list(number)"
]
},
{
"cell_type": "markdown",
"id": "a5f6f56b-0678-4f82-9539-ae7c798d6c63",
"metadata": {},
"source": [
"## Check if number guessed is correct"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 51,
"id": "e6e0b1c2-7c4b-4538-8421-bfdd027b1857", "id": "6194de2b-2dbe-4c49-bc00-e5bed3c35f1f",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a three digit number: 123\n",
"Enter a three digit number: 123\n",
"Enter a three digit number: 345\n",
"Enter a three digit number: 23\n",
"Enter a three digit number: 100\n",
"Enter a three digit number: 675\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You win\n"
]
}
],
"source": [
"while finished == False:\n",
" guess = input(\"Enter a three digit number:\")\n",
" # covert to integer\n",
" guess = int(guess)675\n",
" if guess == number:\n",
" print(\"You win\")\n",
" finished = True"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "232eb403-b705-447e-8d16-0909c4e63e6a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"707\n"
]
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": 9,
"id": "12d5c294-7fc8-47ad-a3bd-45d10bbed198",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['7', '0', '7']\n"
]
}
],
"source": [
"number_parts = [s for s in str(number)]\n",
"print(number_parts)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "a4d0cfc2-34a1-43e8-8c91-5fe2918a4e9e",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdin", "name": "stdin",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Enter a three digit number: \n",
"Enter a three digit number: 123\n" "Enter a three digit number: 123\n"
] ]
}, },
@ -401,97 +441,88 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"1 2 3 \n", "You guessed wrong\n"
"P \n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a three digit number: 456\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"4 5 6 \n",
" \n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a three digit number: 789\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"7 8 9 \n",
"P P \n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a three digit number: 817\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You win!\n"
] ]
} }
], ],
"source": [] "source": [
"guess = GetGuess()\n",
"if guess == number:\n",
" print(\"You guessed correctly\")\n",
"else:\n",
" print(\"You guessed wrong\")"
]
},
{
"cell_type": "markdown",
"id": "0496040d-4d2c-4580-9993-d1832568cd6b",
"metadata": {},
"source": [
"## Check Fumi"
]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 55,
"id": "c43c7dc8-93f2-4f61-821f-1f8f0c2aba39", "id": "ab9145db-4662-41ae-ba38-6c66c7402d80",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"9\r" "index 0 value 1\n",
"index 1 value 2\n",
"index 2 value 3\n"
] ]
} }
], ],
"source": [ "source": [
"from time import sleep\n", "for index, value in enumerate(guess):\n",
"for i in range(10):\n", " print(f'index {index} value {value}')"
" print(i, end=\"\\r\")\n",
" sleep(1)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": null,
"id": "f647c557-d247-48b9-8c31-5623dea46336", "id": "f647c557-d247-48b9-8c31-5623dea46336",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"IPython will make a temporary file named: /tmp/ipython_edit_m0vz1ei2/ipython_edit_oww2f6iw.py\n"
]
}
],
"source": [ "source": [
"%edit" "# %load picofumi.py\n",
"import random \n",
"random.seed()\n",
"\n",
"number = random.randrange(100, 999)\n",
"number = list(str(number))\n",
"guesses = 0\n",
"while True:\n",
" \n",
" if guesses == 5:\n",
" print(\"You loose!\")\n",
" for value in number:\n",
" print(value, end=\" \")\n",
" break\n",
" hint = [\" \", \" \", \" \"]\n",
" guess = input(\"Enter a three digit number:\")\n",
" while len(guess) != 3:\n",
" guess = input(\"Enter a three digit number:\")\n",
" \n",
" guess = list(guess)\n",
" if guess == number:\n",
" print(\"You win!\")\n",
" break\n",
" for index, value in enumerate(guess):\n",
" print(value, end=\" \")\n",
" if value in number:\n",
" hint[index] = \"P\"\n",
" print(\"\")\n",
" for index, value in enumerate(guess):\n",
" if number[index] == value:\n",
" hint[index] = \"F\"\n",
" print(hint[index], end=\" \")\n",
" guesses += 1\n",
" print(\"\\n\")"
] ]
}, },
{ {