🐸

逻辑门

Python 基础python-basics-13-logic-gates
奖励: 100 XP
|

逻辑门

古老的金库需要两把钥匙才能打开。或者也许有一把万能钥匙?Hoppy 需要组合条件。

Python 使用 and (且), or (或), 和 not (非) 来组合逻辑。

逻辑咒语

has_key = True
knows_password = True
is_admin = False

# AND: 两边都必须为真

if has_key and knows_password:
  print("允许访问")

# OR: 至少有一边为真

if has_key or is_admin:
  print("可以通过")

你的任务

1
背包检查

has_wandTruehas_magicFalse

2
严格要求

写一个 if 语句检查你是否 同时 拥有魔杖 and 魔法。如果是,打印 "施放法术"

3
宽松要求

写另一个 if (与第一个分开) 检查你是否拥有魔杖 or 魔法中的 任意一个。如果是,打印 "可以练习"

参考答案
点击展开

逻辑如下:

has_wand = True
has_magic = False

if has_wand and has_magic:
  print("施放法术")

if has_wand or has_magic:
  print("可以练习")
高级技巧
想更进一步?点击展开

你可以把它们串起来!但要注意顺序。括号 () 可以帮助理清关系。

if (has_wand and has_magic) or is_admin:
  print("超级权限")
pymain.py
Loading...
终端输出
Terminal
Ready to run...