用布林運算子做否定
在解決較複雜的問題時,常需要組合布林運算。假設你需要判斷今天是否要下載收盤股價。你只想在市場已經收盤的情況下才進行。收盤價是存放在變數 closing_prices 中的清單。提供的變數 market_closed 會在市場收盤後設為 True。請運用布林運算與物件在布林語境中的評估方式,來判斷是否應該下載資料。
本練習屬於課程
Intermediate Python for Finance
練習說明
- 如果你沒有今天的收盤價,將變數
not_prices設為True。 - 如果市場已收盤且你還沒有收盤價,將變數
get_prices設為True。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
print(closing_prices)
# Assigning True if we need to get the prices
not_prices = ____ closing_prices
print(not_prices)
# Get prices if market is closed and we don't have prices
get_prices = ____ (market_closed ____ not_prices)
print(get_prices)