开始使用免费开始使用

使用布尔运算符取反

在解决更复杂的问题时,您经常需要组合布尔运算。假设您需要判断今天是否要下载股票的收盘价。只有当市场已经收盘时,您才想执行该操作。收盘价存放在提供的变量 closing_prices(列表)中。提供的变量 market_closed 在市场收盘后会被设为 True。请根据布尔运算以及对象在其中的求值规则,确定您是否应该下载数据。

本练习是课程的一部分

Python 金融进阶

查看课程

练习说明

  • 如果您今天没有收盘价,请将变量 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)
编辑并运行代码