Skip to content
Open

lab 2 #705

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions Final_Final_Project_1_week_1_Ironhackbootcamp.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMHF24PGzfLpGfvMSKW960i"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"source": [
"import random\n",
"\n",
"\n",
"def number_guessing_game():\n",
" \"\"\"\n",
" Runs a playable number guessing game.\n",
"\n",
" The player has 10 attempts to guess a random number between 1 and 100.\n",
" The program uses a list to store guesses, a dictionary to store game\n",
" statistics, loops for repeated attempts, flow control for decisions,\n",
" the random module, and try/except for error handling.\n",
" \"\"\"\n",
"\n",
" print(\"Welcome to the Number Guessing Game!\")\n",
" print(\"I'm thinking of a number between 1 and 100.\")\n",
" print(\"You have 10 attempts to guess it.\")\n",
"\n",
" secret_number = random.randint(1, 100)\n",
" max_attempts = 10\n",
"\n",
" guesses = []\n",
"\n",
" game_stats = {\n",
" \"attempts_used\": 0,\n",
" \"won\": False\n",
" }\n",
"\n",
" for attempt in range(1, max_attempts + 1):\n",
" try:\n",
" guess = int(input(\"Enter your guess: \"))\n",
" except ValueError:\n",
" print(\"Please enter a valid number.\")\n",
" continue\n",
"\n",
" guesses.append(guess)\n",
" game_stats[\"attempts_used\"] += 1\n",
"\n",
" if guess < 1 or guess > 100:\n",
" print(\"Please guess a number between 1 and 100.\")\n",
" continue\n",
"\n",
" if guess < secret_number:\n",
" print(\"Too low!\")\n",
" elif guess > secret_number:\n",
" print(\"Too high!\")\n",
" else:\n",
" game_stats[\"won\"] = True\n",
" print(f\"You win! You guessed the number in {game_stats['attempts_used']} attempts.\")\n",
" break\n",
"\n",
" print(f\"Attempts left: {max_attempts - game_stats['attempts_used']}\")\n",
"\n",
" if not game_stats[\"won\"]:\n",
" print(f\"Out of attempts! The number was {secret_number}.\")\n",
" print(\"Better luck next time!\")\n",
"\n",
" print(f\"Your guesses were: {guesses}\")\n",
" print(f\"Game statistics: {game_stats}\")\n",
"\n",
"\n",
"number_guessing_game()"
],
"metadata": {
"id": "eayVC6ifAYJi"
},
"execution_count": null,
"outputs": []
}
]
}
83 changes: 81 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,90 @@
"\n",
"3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8b0b5b1f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Customer Orders:\n",
"{'t-shirt', 'hat', 'mug'}\n",
"\n",
"Order Statistics\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 9\n",
"mug: 4\n",
"hat: 7\n",
"book: 3\n",
"keychain: 12\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = {}\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
"customer_orders = set()\n",
"\n",
"while True:\n",
"\n",
" order = input(\"Enter a product to order: \")\n",
"\n",
" if order in products:\n",
" customer_orders.add(order)\n",
"\n",
" another_product = input(\n",
" \"Do you want to add another product? (yes/no): \"\n",
" )\n",
"\n",
" if another_product.lower() == \"no\":\n",
" break\n",
"\n",
"print(\"\\nCustomer Orders:\")\n",
"print(customer_orders)\n",
"\n",
"total_products_ordered = len(customer_orders)\n",
"\n",
"percentage_ordered = (\n",
" total_products_ordered / len(products)\n",
") * 100\n",
"\n",
"order_status = (\n",
" total_products_ordered,\n",
" percentage_ordered\n",
")\n",
"\n",
"print(\"\\nOrder Statistics\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]}%\")\n",
"\n",
"for product in customer_orders:\n",
" inventory[product] -= 1\n",
"\n",
"print(\"\\nUpdated Inventory:\")\n",
"\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +134,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down