{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "1c2576af", "metadata": { "id": "1c2576af" }, "source": [ "# Estructura condicional: ```if``` ... ```else```" ] }, { "cell_type": "code", "execution_count": 1, "id": "e914c035", "metadata": { "id": "e914c035", "outputId": "e84aff81-f927-4772-f8a1-20220ee6918e", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "n es un número par\n" ] } ], "source": [ "n = 10\n", "if n % 2 == 0:\n", " print(\"n es un número par\")\n", "else:\n", " print(\"n es un número impar\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "910b7f32", "metadata": { "id": "910b7f32", "outputId": "a73cc8a0-cf22-42ab-ebd6-e86b6903066a", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Su velocidad está dentro de los límites legales\n" ] } ], "source": [ "velocidad = 110\n", "limite = 120\n", "if velocidad <= limite:\n", " print(\"Su velocidad está dentro de los límites legales\")\n", "else:\n", " print(\"Su velocidad excede los límites legales\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "6ec332f0", "metadata": { "id": "6ec332f0", "outputId": "a85cb688-27a9-4f37-e2a2-badbd2c53020", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Introduzca la palabra clave: 1234\n", "¡Contraseña correcta!\n", "Acceso permitido.\n" ] } ], "source": [ "clave = input(\"Introduzca la palabra clave: \")\n", "if clave == '1234':\n", " print(\"¡Contraseña correcta!\")\n", " print(\"Acceso permitido.\")\n", "else:\n", " print(\"¡Contraseña incorrecta!\")\n", " print(\"Acceso denegado.\")" ] }, { "cell_type": "markdown", "source": [ "### Pagar las horas a distinto precio\n", "Un trabajador recibe una retribución de 25 €/h por las primeras 20 horas trabajadas y una retribución de 30 €/h por cada hora adicional.\n", "Calcular su salario semanal." ], "metadata": { "id": "qsMcTfCvWLs2" }, "id": "qsMcTfCvWLs2" }, { "cell_type": "markdown", "source": [ "#### Método 1. Usando `else`" ], "metadata": { "id": "d9Zx-3vVY52x" }, "id": "d9Zx-3vVY52x" }, { "cell_type": "code", "source": [ "horas = 22\n", "\n", "if horas <= 20:\n", " salario = horas*25\n", "else:\n", " salario = 20*25 + (horas-20)*30\n", "\n", "print(f\"Trabajando {horas} horas el salario es de {salario} €.\")" ], "metadata": { "id": "5FvxbHKDWx8v", "outputId": "48ef14c5-f39d-4723-9932-8526fb92742f", "colab": { "base_uri": "https://localhost:8080/" } }, "id": "5FvxbHKDWx8v", "execution_count": 5, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Trabajando 22 horas el salario es de 560 €.\n" ] } ] }, { "cell_type": "markdown", "source": [ "#### Método 2. Sin usar `else`" ], "metadata": { "id": "7L8ylREIX6AZ" }, "id": "7L8ylREIX6AZ" }, { "cell_type": "code", "source": [ "horas = 22\n", "print(f\"Trabajando {horas} horas \", end=\"\")\n", "salario = 0\n", "\n", "if horas > 20:\n", " salario = (horas-20)*30\n", " horas = 20\n", "\n", "salario += horas*25\n", "\n", "print(f\"el salario es de {salario} €.\")" ], "metadata": { "id": "4wEUeMwNYFzg", "outputId": "6a98499a-ad8f-40ec-fe67-4ad7f1c35401", "colab": { "base_uri": "https://localhost:8080/" } }, "id": "4wEUeMwNYFzg", "execution_count": 6, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Trabajando 22 horas el salario es de 560 €.\n" ] } ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.8" }, "colab": { "name": "0300_condicional_if_else.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }