PicoFumi/notebooks/picofumi.ipynb

617 lines
11 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "571ac5d7",
"metadata": {},
"source": [
"# Pico Fumi"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72ae05ab",
"metadata": {},
"outputs": [],
"source": [
"%run picofumi.py"
]
},
{
"cell_type": "markdown",
"id": "7760dfcf",
"metadata": {},
"source": [
"## Printing"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "1eabbdcf",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pico Fumi\n"
]
}
],
"source": [
"print(\"Pico Fumi\")"
]
},
{
"cell_type": "markdown",
"id": "c4d898da",
"metadata": {},
"source": [
"## Variables"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c9aa132b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123\n"
]
}
],
"source": [
"#string\n",
"guess = \"123\"\n",
"print(guess)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "64ae1dc1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
}
],
"source": [
"#integer \n",
"guesses = 0\n",
"print(guesses)"
]
},
{
"cell_type": "markdown",
"id": "0cd9ab76",
"metadata": {},
"source": [
"## Get User Input"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "4524a662",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter a three digit number:123\n"
]
}
],
"source": [
"guess = input(\"Enter a three digit number:\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "cbaea031",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123\n"
]
}
],
"source": [
"print(guess)"
]
},
{
"cell_type": "markdown",
"id": "402a7d7c",
"metadata": {},
"source": [
"## Check that the guess a 3 digit number"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "de4d4fbb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(guess)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f747159b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(guess) == 3"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "b82b11f8",
"metadata": {},
"outputs": [],
"source": [
"if len(guess) != 3:\n",
" print(\"Guess is not 3 digits\")"
]
},
{
"cell_type": "markdown",
"id": "9cf640e2",
"metadata": {},
"source": [
"## Re-prompt the user until they enter a 3 digit number"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "3e42feef",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter a three digit number:123\n"
]
}
],
"source": [
"guess = input(\"Enter a three digit number:\")\n",
"while len(guess) != 3:\n",
" guess = input(\"Enter a three digit number:\")"
]
},
{
"cell_type": "markdown",
"id": "5871ca62",
"metadata": {},
"source": [
"## Break guess into 3 parts"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "f0823ff9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['1', '2', '3']\n",
"1\n",
"3\n"
]
}
],
"source": [
"guess = list(guess)\n",
"print(guess)\n",
"print(guess[0])\n",
"print(guess[2])"
]
},
{
"cell_type": "markdown",
"id": "8b32aac0",
"metadata": {},
"source": [
"## Itterate over the guess"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d2c97cb7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"for part in guess:\n",
" print(part)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "cdb0cd32",
"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": "f43efa21",
"metadata": {},
"source": [
"## Functions"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "4fdded17",
"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": 14,
"id": "886517d8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter a three digit number:123\n"
]
},
{
"data": {
"text/plain": [
"['1', '2', '3']"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"GetGuess()"
]
},
{
"cell_type": "markdown",
"id": "b18f748b",
"metadata": {},
"source": [
"## Random Numbers"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "785a2c42",
"metadata": {},
"outputs": [],
"source": [
"import random \n",
"random.seed()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d1778d13",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"437\n"
]
}
],
"source": [
"number = random.randrange(100,999)\n",
"print(number)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "cd137952",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(number)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a270063a",
"metadata": {},
"outputs": [],
"source": [
"#covert to a string\n",
"number = str(number)\n",
"#split into 3 parts\n",
"number = list(number)"
]
},
{
"cell_type": "markdown",
"id": "727a1c33",
"metadata": {},
"source": [
"## Check if number guessed is correct"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "dc005fde",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter a three digit number:472\n",
"You guessed wrong\n"
]
}
],
"source": [
"guess = GetGuess()\n",
"if guess == number:\n",
" print(\"You guessed correctly\")\n",
"else:\n",
" print(\"You guessed wrong\")"
]
},
{
"cell_type": "markdown",
"id": "f0aef1a9",
"metadata": {},
"source": [
"## Check Pico"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "c3752fcd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['4', '7', '2']\n",
"['P', 'P', '']\n"
]
}
],
"source": [
"hint = [\"\", \"\", \"\"]\n",
"for index, value in enumerate(guess):\n",
" if value in number:\n",
" #if value in number and value != number[index]:\n",
" hint[index] = \"P\"\n",
"print(guess)\n",
"print(hint)"
]
},
{
"cell_type": "markdown",
"id": "8ffb6ece",
"metadata": {},
"source": [
"## Check Fumi"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "6591943b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['4', '7', '2']\n",
"['F', 'P', '']\n"
]
}
],
"source": [
"for index, value in enumerate(guess):\n",
" if value == number[index]:\n",
" hint[index] = \"F\"\n",
"print(guess)\n",
"print(hint)"
]
},
{
"cell_type": "markdown",
"id": "f19d453f",
"metadata": {},
"source": [
"## Putting it together"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa759ece",
"metadata": {},
"outputs": [],
"source": [
"# %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\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39b1ea60",
"metadata": {},
"outputs": [],
"source": [
"ls\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.7"
},
"nteract": {
"version": "nteract-on-jupyter@2.1.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}