Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Nested and Complex Conditional Expressions | Villkorssatser
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Introduktion till Python

bookNested and Complex Conditional Expressions

In real-world scenarios like managing a grocery store, you often need to make decisions that depend on several factors. Nested and complex conditional expressions in Python let you handle such situations by checking multiple conditions in a structured way.

To write a nested if-else statement in Python, place one if-else block inside another. Use a colon (:) after each if, elif, or else keyword to indicate the start of a code block. Indent the code inside each block to show which statements belong to which condition. This structure keeps your logic clear and ensures your code runs as you intend.

if condition:
    if nested_condition:
        code_block
    else:
        code_block
else:
    code_block # or another nested condition

Here is a clear example of how to use nested if-else statements in Python, following the grocery store theme. Notice the use of colons (:) and indentation to structure the decision logic:

12345678910
product_type = "Perishable" days_in_stock = 6 if product_type == "Perishable": if days_in_stock > 5: print("Product is old - Apply 50% discount") else: print("Product is fresh - Full price") else: print("Non-perishable item - No freshness check needed")
copy

This code checks if a product is perishable. If it is, it further checks how many days it has been in stock to decide on the discount. Indentation and colons are essential for defining which code belongs to each condition.

Consider a situation where you need to decide how to handle different products based on both their type and how many units are left in stock. For instance, you want to alert staff when a perishable item is running low or when a non-perishable item is out of stock. Here is how you can use nested if-else statements to organize this logic clearly:

1234567891011121314151617
product_type = "Non-perishable" days_in_stock = 12 stock_quantity = 3 if product_type == "Perishable": if days_in_stock > 5: print("Perishable and old - Apply 50% discount") elif stock_quantity < 5: print("Perishable and low stock - Consider restocking") else: print("Perishable and fresh - Full price") else: if stock_quantity < 5: print("Non-perishable and low stock - Restock soon") else: print("Non-perishable and stock is sufficient")
copy

This example checks the product_type. If it is perishable, it further checks if the stock level is low and prints a restocking alert if needed. If the item is non-perishable, it checks if the stock is empty and signals a reorder. Using nested if-else statements like this helps you make clear, step-by-step decisions based on multiple factors.

Uppgift

Swipe to start coding

You are managing inventory for a grocery store. Your task is to write a nested if-else statement to decide what message to print based on the following criteria:

  • If a product is Perishable:
    • If it has been in stock for more than 7 days, print Perishable and very old - Apply 70% discount.
    • Otherwise, if the stock quantity is less than 3, print Perishable and low stock - Restock immediately.
    • Otherwise, print Perishable and fresh - Full price.
  • If a product is Non-perishable:
    • If the stock quantity is 0, print Non-perishable and out of stock - Reorder now.
    • Otherwise, print Non-perishable and stock is sufficient.

Write your nested if-else statement using the variables product_type, days_in_stock, and stock_quantity. Test your code by running it with the provided variable values and make sure it prints the correct message for each scenario.

Lösning

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 7
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain how the nested if-else structure works in the second example?

What happens if the stock quantity is higher, say 10, for a non-perishable item?

Can you show how to add another condition, like checking for an "expiring soon" status?

close

bookNested and Complex Conditional Expressions

Svep för att visa menyn

In real-world scenarios like managing a grocery store, you often need to make decisions that depend on several factors. Nested and complex conditional expressions in Python let you handle such situations by checking multiple conditions in a structured way.

To write a nested if-else statement in Python, place one if-else block inside another. Use a colon (:) after each if, elif, or else keyword to indicate the start of a code block. Indent the code inside each block to show which statements belong to which condition. This structure keeps your logic clear and ensures your code runs as you intend.

if condition:
    if nested_condition:
        code_block
    else:
        code_block
else:
    code_block # or another nested condition

Here is a clear example of how to use nested if-else statements in Python, following the grocery store theme. Notice the use of colons (:) and indentation to structure the decision logic:

12345678910
product_type = "Perishable" days_in_stock = 6 if product_type == "Perishable": if days_in_stock > 5: print("Product is old - Apply 50% discount") else: print("Product is fresh - Full price") else: print("Non-perishable item - No freshness check needed")
copy

This code checks if a product is perishable. If it is, it further checks how many days it has been in stock to decide on the discount. Indentation and colons are essential for defining which code belongs to each condition.

Consider a situation where you need to decide how to handle different products based on both their type and how many units are left in stock. For instance, you want to alert staff when a perishable item is running low or when a non-perishable item is out of stock. Here is how you can use nested if-else statements to organize this logic clearly:

1234567891011121314151617
product_type = "Non-perishable" days_in_stock = 12 stock_quantity = 3 if product_type == "Perishable": if days_in_stock > 5: print("Perishable and old - Apply 50% discount") elif stock_quantity < 5: print("Perishable and low stock - Consider restocking") else: print("Perishable and fresh - Full price") else: if stock_quantity < 5: print("Non-perishable and low stock - Restock soon") else: print("Non-perishable and stock is sufficient")
copy

This example checks the product_type. If it is perishable, it further checks if the stock level is low and prints a restocking alert if needed. If the item is non-perishable, it checks if the stock is empty and signals a reorder. Using nested if-else statements like this helps you make clear, step-by-step decisions based on multiple factors.

Uppgift

Swipe to start coding

You are managing inventory for a grocery store. Your task is to write a nested if-else statement to decide what message to print based on the following criteria:

  • If a product is Perishable:
    • If it has been in stock for more than 7 days, print Perishable and very old - Apply 70% discount.
    • Otherwise, if the stock quantity is less than 3, print Perishable and low stock - Restock immediately.
    • Otherwise, print Perishable and fresh - Full price.
  • If a product is Non-perishable:
    • If the stock quantity is 0, print Non-perishable and out of stock - Reorder now.
    • Otherwise, print Non-perishable and stock is sufficient.

Write your nested if-else statement using the variables product_type, days_in_stock, and stock_quantity. Test your code by running it with the provided variable values and make sure it prints the correct message for each scenario.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 7
single

single

some-alt