{"id":"logic-nested","slug":"nested-conditionals","title":"Conditions inside conditions","text":"The block of an `if` can contain any statements, another `if` among them. A conditional statement inside another one is a **nested conditional**. Here the outer `if` checks that the price makes sense at all, and the inner one whether there's enough money:\n\n```python\nbalance = 50\nprice = int(input(\"Price: \"))\nif price > 0:\n    if price <= balance:\n        print(\"Purchase approved.\")\n    else:\n        print(\"Not enough money.\")\nelse:\n    print(\"That's not a valid price.\")\n```\n\n```input\n80\n```\n\n```output\nPrice: 80\nNot enough money.\n```\n\nThe inner statement is checked only when the outer condition is true. With a price of -5, Python goes straight to the outer `else`.\n\n### The indentation decides\n\nIn nested statements the indentation shows which `else` belongs to which `if`: an `else` belongs to the `if` above it at the same indentation. Move the inner `else` four spaces to the left, and the program means something else:\n\n```python\nbalance = 50\nprice = int(input(\"Price: \"))\nif price > 0:\n    if price <= balance:\n        print(\"Purchase approved.\")\nelse:\n    print(\"Not enough money.\")\n```\n\n```input\n80\n```\n\n```output\nPrice: 80\n```\n\nNow the `else` belongs to the outer `if`. A price of 80 prints nothing at all, and a price of -5 would say `Not enough money.` Python has no way to know which one you meant, so get the indentation right.\n\n### Nested or combined?\n\nThe same decision can often be written with `and` instead of nesting:\n\n```python\nbalance = 50\nprice = 30\nif price > 0 and price <= balance:\n    print(\"Purchase approved.\")\nelif price > 0:\n    print(\"Not enough money.\")\nelse:\n    print(\"That's not a valid price.\")\n```\n\n```output\nPurchase approved.\n```\n\nPick whichever version is easier to follow. Nesting fits well when a second question only makes sense after the first one, for example when the program asks for more input in only one branch. A combined condition keeps short checks flat and easy to scan.\n","task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"weight = 3\ndestination = \"home\"\nif destination == \"home\":\n    if weight <= 2:\n        print(\"Small parcel: 5 euros\")\n    else:\n        print(\"Large parcel: 9 euros\")\nelse:\n    print(\"Abroad: 15 euros\")\nprint(\"Label printed\")\n","solution":"weight = 3\ndestination = \"home\"\nif destination == \"home\":\n    if weight <= 2:\n        print(\"Small parcel: 5 euros\")\n    else:\n        print(\"Large parcel: 9 euros\")\nelse:\n    print(\"Abroad: 15 euros\")\nprint(\"Label printed\")\n","hints":["The outer condition `destination == \"home\"` is true, so Python goes into its block and checks the inner `if`.","3 is more than 2, so the inner `else` runs. The outer `else` is skipped, and the last line is in the main block."],"tests":[{"name":"Output","stdin":"","expect":"Large parcel: 9 euros\nLabel printed","hidden":false}],"require":[],"forbid":[],"lines":["Large parcel: 9 euros","Label printed"],"more":[{"task":"The program should say `Take an umbrella.` when it's warm (15 degrees or more) and raining, and `Enjoy the sunshine!` when it's warm and not raining. When it's colder, it should say `Stay inside and read a book.`, rain or not. One `else` is in the wrong place, and one branch is missing. Fix the program.\n\n```\nTemperature: 20\nIs it raining? (yes/no) no\nEnjoy the sunshine!\n```\n","kind":"code","seed":"temperature = int(input(\"Temperature: \"))\nraining = input(\"Is it raining? (yes/no) \")\nif temperature >= 15:\n    if raining == \"yes\":\n        print(\"Take an umbrella.\")\nelse:\n    print(\"Enjoy the sunshine!\")\n","solution":"temperature = int(input(\"Temperature: \"))\nraining = input(\"Is it raining? (yes/no) \")\nif temperature >= 15:\n    if raining == \"yes\":\n        print(\"Take an umbrella.\")\n    else:\n        print(\"Enjoy the sunshine!\")\nelse:\n    print(\"Stay inside and read a book.\")\n","hints":["`Enjoy the sunshine!` is for warm days without rain, so its `else` belongs to the inner `if raining == \"yes\":`. Indent it to the same level as that `if`.","The cold days need an `else` of their own at the left edge, the same indentation as `if temperature >= 15:`."],"tests":[{"name":"Test 1","stdin":"20\nno\n","expect":"Temperature: 20\nIs it raining? (yes/no) no\nEnjoy the sunshine!","hidden":false},{"name":"Test 2","stdin":"20\nyes\n","expect":"Temperature: 20\nIs it raining? (yes/no) yes\nTake an umbrella.","hidden":true},{"name":"Test 3","stdin":"5\nno\n","expect":"Temperature: 5\nIs it raining? (yes/no) no\nStay inside and read a book.","hidden":true},{"name":"Test 4","stdin":"5\nyes\n","expect":"Temperature: 5\nIs it raining? (yes/no) yes\nStay inside and read a book.","hidden":true},{"name":"Test 5","stdin":"15\nno\n","expect":"Temperature: 15\nIs it raining? (yes/no) no\nEnjoy the sunshine!","hidden":true}],"require":[],"forbid":[]},{"task":"Write a program for a theme park ride. It asks for the rider's height in centimeters. Riders under 120 cm get the message `Sorry, you must be at least 120 cm tall.` Taller riders are asked whether they want the fast lane: `yes` gives `Fast lane ticket: 30 euros`, anything else `Regular ticket: 20 euros`. Only riders who are tall enough are asked the second question.\n\n```\nHeight in cm: 150\nFast lane? (yes/no) yes\nFast lane ticket: 30 euros\n```\n","kind":"code","seed":"height = int(input(\"Height in cm: \"))\n","solution":"height = int(input(\"Height in cm: \"))\nif height >= 120:\n    fast = input(\"Fast lane? (yes/no) \")\n    if fast == \"yes\":\n        print(\"Fast lane ticket: 30 euros\")\n    else:\n        print(\"Regular ticket: 20 euros\")\nelse:\n    print(\"Sorry, you must be at least 120 cm tall.\")\n","hints":["Start with `if height >= 120:` and an `else` for the riders who are too short.","The question about the fast lane is asked inside the first block, so the `input` line is indented there.","After that `input`, a nested `if` and `else` in the same block choose the ticket."],"tests":[{"name":"Test 1","stdin":"150\nyes\n","expect":"Height in cm: 150\nFast lane? (yes/no) yes\nFast lane ticket: 30 euros","hidden":false},{"name":"Test 2","stdin":"150\nno\n","expect":"Height in cm: 150\nFast lane? (yes/no) no\nRegular ticket: 20 euros","hidden":true},{"name":"Test 3","stdin":"110\n","expect":"Height in cm: 110\nSorry, you must be at least 120 cm tall.","hidden":true},{"name":"Test 4","stdin":"120\nyes\n","expect":"Height in cm: 120\nFast lane? (yes/no) yes\nFast lane ticket: 30 euros","hidden":true}],"require":[],"forbid":[]}]}