Kombinera Villkor
Genom att bygga vidare på din förståelse av booleska värden kommer vi nu att utforska hur man kombinerar flera villkor i Python. Denna färdighet gör det möjligt för dina program att fatta ännu mer nyanserade beslut genom att kontrollera flera kriterier samtidigt. Se hur Alex kombinerar flera villkor för att fatta bättre beslut under arbetet i mataffären:
Förståelse av kombinerade villkor
I Python kan du kombinera villkor med logiska operatorer såsom and
, or
och not
. Dessa operatorer gör det möjligt att skapa sammansatta villkor som utvärderar flera booleska uttryck.
and
: ReturnerarTrue
om båda villkoren ärTrue
;or
: ReturnerarTrue
om minst ett villkor ärTrue
;not
: ReturnerarTrue
om villkoret ärFalse
(och vice versa).
Exempel på tillämpning
Låt oss kombinera villkor för att kontrollera om en vara både är en färskvara OCH har stort lager med hjälp av operatorn and
:
# Define the perishable and stock status conditions is_perishable = True item_quantity = 110 perishable_highStockRisk = 100 # Using the (and) operator to combine two conditions # The first condition (`is_perishable`) checks if the item is perishable # The second condition (`item_quantity >= perishable_highStockRisk`) checks if the item is high in stock # The `consider_discount` variable will become `True` only if both conditions are `True` consider_discount = is_perishable and (item_quantity >= perishable_highStockRisk) # Print the result print("Is the item perishable and high in stock?", consider_discount)
Nu ska vi kombinera villkor för att kontrollera om en vara är antingen en säsongsvara ELLER om det är en högtidsvara med hjälp av or
-operatorn:
# Define the seasonal and holiday status conditions seasonal_item = False holiday_item = True # Combine the conditions to check if the item is seasonal or discounted # (`temporary_stock`) will become `True` if either condition `seasonal_item` OR `holiday_item` is `True` temporary_stock = seasonal_item or holiday_item # Print the result print("Is this a seasonal or holiday item?", temporary_stock)
Slutligen, låt oss kombinera villkor för att kontrollera om en vara INTE behöver omprissättning med hjälp av not
-operatorn:
# Define the item status condition is_perishable = True # Use the `not` operator to check if the item is NOT perishable long_shelf_life = not is_perishable # Print the result print("Does the item need to be sold quickly?", long_shelf_life)
Swipe to start coding
Utvärdera om en vara är rabatterad eller har lågt lagersaldo för att avgöra dess berättigande till kampanj.
- Definiera en boolesk variabel
movingProduct
som ärTrue
om varan är antingen rabatterad eller har lågt lagersaldo, med hjälp av logiska operatorer. - Skapa en boolesk variabel
promotion
som ärTrue
om varan inte är rabatterad och har tillräckligt lagersaldo. - Skriv ut meddelandet:
Is the item eligible for promotion? <promotion>
.
Utmatningskrav
- Skriv ut om varan är berättigad till kampanj:
Is the item eligible for promotion? <promotion>
.
Lösning
Tack för dina kommentarer!