{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "source": [ "# The while loop in Python is used to repeat a block of code as long as a condition is True.\n", "# It’s great when you don’t know ahead of time how many times you need to repeat something." ], "metadata": { "id": "CFUy7TnPKexz" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# 4 steps\n", "i = 1 # initialization\n", "while i <= 7: # condition check\n", " print(i, end = \" \") # body of the loop; end is for not adding a new line\n", " i += 1 # incrementation" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "BgiP2YjVKe0f", "outputId": "a3916c76-f4da-44fd-952e-5f695cfd60f3" }, "execution_count": 1, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1 2 3 4 5 6 7 " ] } ] }, { "cell_type": "code", "source": [ "# decreasing while loop\n", "i = 7\n", "while i > 0:\n", " print(i, end = \" \")\n", " i -= 1" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7b91LglHYIyN", "outputId": "6b99ab4f-0b9d-4af0-8398-6a85d943cefc" }, "execution_count": 2, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "7 6 5 4 3 2 1 " ] } ] }, { "cell_type": "code", "source": [ "i = 1\n", "while i < 11:\n", " if i == 10: # not to add -- after 10\n", " print(i)\n", " else:\n", " print(i, end = \"--\")\n", " i += 1" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "uK_MReENYEst", "outputId": "95f9a459-56f9-4e9b-f336-39896e109e76" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1--2--3--4--5--6--7--8--9--10\n" ] } ] }, { "cell_type": "code", "source": [ "# Common Mistake: Infinite Loop\n", "while True:\n", " print(\"♾️\") # If the condition never becomes False, the loop will run forever" ], "metadata": { "id": "IUb4hbzCKe3Y" }, "execution_count": 3, "outputs": [] }, { "cell_type": "code", "source": [ "# while with break\n", "i = 1\n", "while i <= 10:\n", " print(i)\n", " if i == 5:\n", " break # exits the loop\n", " i += 1" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "TWE-fvcBKe6K", "outputId": "b2da028c-d0a9-4dee-80d5-462e9b650ea2" }, "execution_count": 4, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n" ] } ] }, { "cell_type": "code", "source": [ "# while with else - while else suite\n", "x = 1\n", "while x < 4:\n", " print(x)\n", " x += 1\n", "else:\n", " print(\"Loop finished!\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "QJ5zIFr4Ke84", "outputId": "1b8415d8-19e7-4a23-b559-31dc305d6e1e" }, "execution_count": 5, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1\n", "2\n", "3\n", "Loop finished!\n" ] } ] }, { "cell_type": "code", "source": [ "# continue in a while loop\n", "i = 0\n", "while i < 10:\n", " i += 1\n", " if i % 3 == 0:\n", " continue # skip the iteration when i is a multiple of 3\n", " print(i, end= \" \")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dX-DMUG0Ke_g", "outputId": "77047dfc-55cb-42df-97d8-376bafb76b9a" }, "execution_count": 6, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1 2 4 5 7 8 10 " ] } ] }, { "cell_type": "code", "source": [ "# Using input() to control the loop\n", "while True:\n", " name = input(\"Enter your name (or type 'exit'): \")\n", " if name == 'exit':\n", " break\n", " print(f\"Hello, {name}!\")\n", "else:\n", " print(\"Not executed\") # when the break statement gets executed; this else is skipped\n", "print(\"Out of loop\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "vQVFX_RGKfCH", "outputId": "4100c6f8-eb10-4b0c-f9b2-9685345dcd0a" }, "execution_count": 7, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter your name (or type 'exit'): Dhoni\n", "Hello, Dhoni!\n", "Enter your name (or type 'exit'): Raina\n", "Hello, Raina!\n", "Enter your name (or type 'exit'): exit\n", "Out of loop\n" ] } ] }, { "cell_type": "code", "source": [ "# Simulating countdowns or timers\n", "import time\n", "\n", "count = 7\n", "while count > 0:\n", " print(f\"Countdown: {count}\")\n", " time.sleep(1) # pauses for 1 second\n", " count -= 1\n", "print(\"Hip Hip Hurray!\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "WzwcX7jUKfHp", "outputId": "da6f9245-5892-4dc9-9586-43a7717f8c89" }, "execution_count": 8, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Countdown: 7\n", "Countdown: 6\n", "Countdown: 5\n", "Countdown: 4\n", "Countdown: 3\n", "Countdown: 2\n", "Countdown: 1\n", "Hip Hip Hurray!\n" ] } ] }, { "cell_type": "code", "source": [ "# Waiting for a condition to be met\n", "password = \"\"\n", "while password != \"python123\":\n", " password = input(\"Enter password: \")\n", "print(\"Access granted!\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "HECabNNZKfKY", "outputId": "56b9fe6d-dab1-4194-daaa-51971ee45cf5" }, "execution_count": 9, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter password: Python\n", "Enter password: 123\n", "Enter password: Python 123\n", "Enter password: python123\n", "Access granted!\n" ] } ] }, { "cell_type": "code", "source": [ "# Useful in games, simulations, or retry logic\n", "secret = 7\n", "guess = 0\n", "while guess != secret:\n", " guess = int(input(\"Guess the number: \"))\n", "print(\"You guessed it!\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8WjwN2otKfNQ", "outputId": "b0f183b1-0f3b-45e7-b306-7f37b7435173" }, "execution_count": 10, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Guess the number: 1\n", "Guess the number: 5\n", "Guess the number: 7\n", "You guessed it!\n" ] } ] }, { "cell_type": "code", "source": [ "# Number Guessing Game\n", "\n", "import random\n", "\n", "secret_number = random.randint(1, 10)\n", "attempts = 0\n", "max_attempts = 3\n", "\n", "print(\"Welcome to the Number Guessing Game!\")\n", "print(\"Guess a number between 1 and 10.\")\n", "print(f\"You have only {max_attempts} chances!\")\n", "\n", "while attempts < max_attempts:\n", " guess = int(input(\"\\nYour guess: \"))\n", " attempts += 1\n", "\n", " if guess == secret_number:\n", " print(\"🎉 Correct! You guessed the number.\")\n", " break\n", " elif guess < secret_number:\n", " print(\"Too low!\")\n", " else:\n", " print(\"Too high!\")\n", "\n", " if attempts < max_attempts:\n", " print(f\"You have {max_attempts - attempts} tries left.\")\n", " else:\n", " print(f\"\\n😢 Out of chances! The correct number was {secret_number}.\")\n", "" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "oaQfk7KjKfQI", "outputId": "bad9dc08-eaf4-43ef-86d3-d86cd1f93f32" }, "execution_count": 11, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Welcome to the Number Guessing Game!\n", "Guess a number between 1 and 10.\n", "You have only 3 chances!\n", "\n", "Your guess: 5\n", "Too high!\n", "You have 2 tries left.\n", "\n", "Your guess: 3\n", "Too low!\n", "You have 1 tries left.\n", "\n", "Your guess: 4\n", "🎉 Correct! You guessed the number.\n" ] } ] }, { "cell_type": "code", "source": [ "# Billing - Super Market\n", "print(\"Express Counter at the Supermarket...\")\n", "name = input(\"Enter the name: \")\n", "address = input(\"Enter the address: \")\n", "ch = int(input(\"Are you sure that you have items less than or equal to 10? Type 1 for Yes or 2 for No: \"))\n", "\n", "if ch == 1:\n", " ctr = 0\n", " total_price = 0\n", " item_id = 1\n", " while ctr <= 10:\n", " print(f\"\\nEnter the price of item {item_id}\")\n", "\n", " price = float(input(\"₹ \"))\n", "\n", " if price <= 0:\n", " print(\"Invalid price. Please enter a valid price.\")\n", " continue # Skip to the next iteration without incrementing ctr\n", "\n", " total_price += price\n", " item_id += 1\n", " ctr += 1 # Only increment ctr and item_id if the price is valid\n", " print(f\"Your bill so far for {ctr} items is ₹ {total_price}.\")\n", "\n", " choice = input(\"\\nEnter 'q' to quit or press 'Enter' to continue..\")\n", " if choice == 'q':\n", " break\n", "\n", " print(\"\\n----- BILL -----\")\n", " print(f\"Customer Name: {name}\")\n", " print(f\"Customer Address: {address}\")\n", " print(f\"\\nYou have purchased {ctr} item(s).\")\n", " print(f\"Total Bill: ₹ {total_price:.2f}\")\n", " print(\"------------------\")\n", "\n", "else:\n", " print(\"Please check the other counter.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Suo6oga6Ri8-", "outputId": "9b246cd6-f1fe-456c-a0a0-c5e1317259b2" }, "execution_count": 12, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Express Counter at the Supermarket...\n", "Enter the name: Sakshi\n", "Enter the address: Ranchi\n", "Are you sure that you have items less than or equal to 10? Type 1 for Yes or 2 for No: 1\n", "\n", "Enter the price of item 1\n", "₹ 100\n", "Your bill so far for 1 items is ₹ 100.0.\n", "\n", "Enter 'q' to quit or press 'Enter' to continue..\n", "\n", "Enter the price of item 2\n", "₹ 50\n", "Your bill so far for 2 items is ₹ 150.0.\n", "\n", "Enter 'q' to quit or press 'Enter' to continue..\n", "\n", "Enter the price of item 3\n", "₹ -25\n", "Invalid price. Please enter a valid price.\n", "\n", "Enter the price of item 3\n", "₹ 25\n", "Your bill so far for 3 items is ₹ 175.0.\n", "\n", "Enter 'q' to quit or press 'Enter' to continue..\n", "\n", "Enter the price of item 4\n", "₹ 0\n", "Invalid price. Please enter a valid price.\n", "\n", "Enter the price of item 4\n", "₹ 10\n", "Your bill so far for 4 items is ₹ 185.0.\n", "\n", "Enter 'q' to quit or press 'Enter' to continue..q\n", "\n", "----- BILL -----\n", "Customer Name: Sakshi\n", "Customer Address: Ranchi\n", "\n", "You have purchased 4 item(s).\n", "Total Bill: ₹ 185.00\n", "------------------\n" ] } ] } ] }