Created
July 8, 2023 08:23
-
-
Save kenresoft/45094ab7b60ae477ad4d18694fff90fe to your computer and use it in GitHub Desktop.
python-functions.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/kenresoft/45094ab7b60ae477ad4d18694fff90fe/python-functions.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "PTuvOIBckR38" | |
}, | |
"source": [ | |
"# python-functions\n", | |
"\n", | |
"Use the \"Run\" button to execute the code." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "inA-sW_qkR3-", | |
"outputId": "d4500bcc-ee4b-4336-dd7d-8c76304d34d5" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Enter your chosen numbers (separated by comma): 1,2,3,4,5,6\n", | |
"Enter the drawn numbers (separated by comma): 2,5,6,8\n", | |
"\n", | |
"Your chosen numbers are: ['1', '2', '3', '4', '5', '6']\n", | |
"\n", | |
"The drawned numbers are: ['2', '5', '6', '8']\n", | |
"\n", | |
"You hit 3 number(s) --- ['2', '5', '6']\n" | |
] | |
} | |
], | |
"source": [ | |
"input_function()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "3REsmcaakR3_" | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"id": "rhrT20QVkR3_" | |
}, | |
"outputs": [], | |
"source": [ | |
"def input_function():\n", | |
" # Retrive the chosen numbers from the user and turn them to a list.\n", | |
" chosen = input(\"Enter your chosen numbers (separated by comma): \").split(',')\n", | |
"\n", | |
" # Retrive the drawn numbers from the user and turn them to a list.\n", | |
" drawn = input(\"Enter the drawn numbers (separated by comma): \").split(',')\n", | |
"\n", | |
" main_function(chosen, drawn)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "SD8mdGSIkR4A" | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"id": "rU-kMsNHkR4A" | |
}, | |
"outputs": [], | |
"source": [ | |
"def main_function(chosen_numbers, drawn_numbers):\n", | |
" hits = 0\n", | |
" hit_numbers = []\n", | |
" # Check if input is not empty\n", | |
" if chosen_numbers == [] or drawn_numbers == []:\n", | |
" print(\"Please enter both chosen and drawn numbers.\")\n", | |
" else:\n", | |
" for number in chosen_numbers:\n", | |
" # If a chosen number is also in a drawn number, increment hit.\n", | |
" if number in drawn_numbers:\n", | |
" # add hit numbers to a new list\n", | |
" hit_numbers.append(number)\n", | |
" hits += 1\n", | |
" print_function(chosen_numbers, drawn_numbers, hits, hit_numbers)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"id": "dWTaXG2LkR4A" | |
}, | |
"outputs": [], | |
"source": [ | |
"def print_function(chosen_numbers_result, drawn_numbers_result, hits, hit_numbers_result):\n", | |
" print(\"\\nYour chosen numbers are:\", chosen_numbers_result)\n", | |
" print(\"\\nThe drawned numbers are:\", drawn_numbers_result)\n", | |
" print(\"\\nYou hit\", hits, \"number(s) --- \", hit_numbers_result)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "Qhu0G33GkR4B" | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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" | |
}, | |
"colab": { | |
"provenance": [], | |
"include_colab_link": true | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment