Kombinera Villkor
Genom att bygga vidare på din förståelse av booleans, kommer vi nu att utforska hur man kombinerar flera villkor i Python. Denna färdighet gör att dina program kan fatta ännu mer nyanserade beslut genom att kontrollera flera kriterier samtidigt. Se när Alex kombinerar flera villkor för att fatta bättre beslut under livsmedelsbutikens verksamhet:
Förståelse av kombinerade villkor
I Python kan du kombinera villkor med logiska operatorer som and
, or
och not
. Dessa operatorer låter dig 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).
Exempelapplikation
Låt oss kombinera villkor för att kontrollera om en vara är både en färskvara OCH har hög lagerstatus med hjälp av and
-operatorn:
# 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, låt oss kombinera villkor för att kontrollera om en vara är antingen en säsongsvara ELLER om det är en helgdag vara 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 artikel är rabatterad eller lågt i lager för att avgöra dess berättigande för kampanj.
- Definiera en boolesk variabel
movingProduct
som ärTrue
om artikeln är antingen rabatterad eller lågt i lager, med hjälp av logiska operatorer. - Skapa en boolesk variabel
promotion
som ärTrue
om artikeln är inte rabatterad och tillräckligt i lager. - Skriv ut meddelandet:
Is the item eligible for promotion? <promotion>
.
Utmatningskrav
- Skriv ut om artikeln är berättigad för kampanj:
Is the item eligible for promotion? <promotion>
.
Lösning
Tack för dina kommentarer!