> For the complete documentation index, see [llms.txt](https://www.pythonclassroom.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pythonclassroom.com/decisions-if-elif-else/nested-if.md).

# nested if

## Objective

After working through this lesson, you’ll be able to

* Write Python coding using nested if statements.

{% embed url="<https://docs.google.com/presentation/d/1-W344Z0blrzMCeEDcuvSMwpAMLP7HoXMXR9mbHE_c48/edit?usp=sharing>" %}

## Introductory Problem

Ask the user if they are hungry. If the user replies yes, print “Goto the grocery store”. Else if the user replies no, print “Stay Home”. Else print “invalid choice”.

```python
hungry = input("Are you hungry? ")
if hungry == "Yes":
   print("Goto the Grocery Store")
elif hungry == "No":
   print("Stay Home")
else:
   print("Invalid choice")
```

## nested if

Sometimes you want to make a decision based on the result of a previous decision. One way to do this in Python is using nested conditionals, a second conditional executes only if the result of the first conditional is True.

![](https://729613953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LVVKHMnpflHwghmbdgi%2F-Le4BE9RbXx49qI74m0D%2F-Le4BJtber2zokuMudGn%2FScreen%20Shot%202019-05-04%20at%207.31.53%20PM.png?alt=media\&token=f604ac55-9c90-4800-b408-8ed81008ce9c)

## Example 1

In the if “Yes” indented block, ask the user how much does chocolate cost. If the user enters a price less than or equal to a dollar, print “buy 3”. Else if the user enters a price more than a dollar, print “buy 1”.

{% code title="example2.py" %}

```python
hungry = input("Are you hungry? ")
if hungry == "Yes":
   print("Goto the Grocery Store")
   cost = float(input("How much does chocolate cost? "))
   if cost <= 1:
      print("Buy 3")
   else:
      print("Buy 1")
elif hungry == "No":
   print("Stay Home")
else:
   print("Invalid choice")
```

{% endcode %}

## Example 2

In the elif “no” indented block, ask the user, do you want to play Fortnite. If the user enters, yes, print “see you tomorrow morning”. Else if the user enters no, print “Go back to studying”.

{% code title="example3.py" %}

```python
hungry = input("Are you hungry? ")
if hungry == "Yes":
   print("Goto the Grocery Store")
   cost = float(input("How much does chocolate cost? "))
   if cost <= 1:
      print("Buy 3")
   else:
      print("Buy 1")
elif hungry == "No":
   print("Stay Home")
   fortnite = input("Do you want to play Fortnite? ")
   if fortnite == "yes":
      print("See you next week")
   elif fortnite == "no":
      print("Go back to studying")
else:
   print("Invalid choice")
```

{% endcode %}

## Example 3

Under print “Go back to studying”, ask the user what their favorite subject is. If the user inputs “History”, print “that’s my favorite subject too!” Else print “we don’t have things in common”.

{% code title="example4.py" %}

```python
hungry = input("Are you hungry? ")
if hungry == "Yes":
   print("Goto the Grocery Store")
   cost = float(input("How much does chocolate cost? "))
   if cost <= 1:
      print("Buy 3")
   else:
      print("Buy 1")
elif hungry == "No":
   print("Stay Home")
   fortnite = input("Do you want to play Fortnite? ")
   if fortnite == "yes":
      print("See you next week")
   elif fortnite == "no":
      print("Go back to studying")
      subject = input("What's your favorite subject? ")
      if subject == "History":
         print("That's my favorite subject too")
      else:
         print("We don't have things in common")
else:
   print("Invalid choice")
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pythonclassroom.com/decisions-if-elif-else/nested-if.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
