网易极客战记计算机科学3攻略,【网易官方】极客战记(codecombat)攻略-森林-逻辑之路logical-path(示例代码)...

d144c8484fd419ba291e06f730c7d9f8.png

使用逻辑跟着巫师来到他的宝藏

简介

584df2b1e00e471a2a74b8f009dbb165.png

在森林深处,一个巫师给了你一次挑战:如果你能解出他的逻辑谜题,他会和你分享他的宝物!

使用 与 (AND), 或 (OR), 非 (NOT) 这种 布尔操作符 (boolean operators) 觉得在每个分叉路口走哪条路。查看提示学会怎么写出你要的代码。

默认代码

# 从巫师那得到两个秘密的真假值

# 查看提示,学会写逻辑表达式。

hero.moveXY(14, 24)

secretA = hero.findNearestFriend().getSecretA()

secretB = hero.findNearestFriend().getSecretB()

# 如果 secretA 和 secretB 都为真,走上面的路;否则,走下面。

secretC = secretA and secretB

if secretC:

hero.moveXY(20, 33)

else:

hero.moveXY(20, 15)

hero.moveXY(26, 24)

# 如果 secretA 和 secretB 中有一个为真,走上面。

hero.moveXY(38, 24)

# 如果 secretB 不是真的,走上面。

hero.moveXY(50, 24)

概览

在这关,你需要按照 布尔值 (真或假) 选择前方的道路。在每个分叉路口,值为真时走上方的路,值为假时走下方的路。巫师会给你前两个值,而后面的则需要你用 布尔运算 得出。

布尔操作符 处理真 (TRUE) 和假 (FALSE) 这两个值,并返回真或假。

与 (AND) 操作符在两个输入都为真时返回真:

# Python 里 ‘and‘ 是小写。‘True‘ 和 ‘False‘ 首字母大写。

a = True and True # a = True

b = True and False # b = False

c = False and True # c = False

d = False and False # d = False

或 (OR) 操作符在两个输入的任意一个为真时返回真:

a = True or True # a = True

b = True or False # b = True

c = False or True # c = True

d = False or False # d = False

非 (NOT) 操作符有点不同,它只处理一个输入,返回相反值:

a = not True # a = False

b = not False # b = True

当然啦,你还可以用布尔运算将变量和布尔值结合:

a = not False # a = True

b = a or False # b = True

c = a and b # c = True

使用这些工具帮你找到最终到达宝藏的路!

逻辑之路 解法

# 从巫师那得到两个秘密的真假值

# 查看提示,学会写逻辑表达式。

hero.moveXY(14, 24)

secretA = hero.findNearestFriend().getSecretA()

secretB = hero.findNearestFriend().getSecretB()

# 如果 secretA 和 secretB 都为真,走上面的路;否则,走下面。

secretC = secretA and secretB

if secretC:

hero.moveXY(20, 33)

else:

hero.moveXY(20, 15)

hero.moveXY(26, 24)

# 如果 secretA 和 secretB 中有一个为真,走上面。

secretD = secretA or secretB

if secretD:

hero.moveXY(32, 33)

else:

hero.moveXY(32, 15)

hero.moveXY(38, 24)

# 如果 secretB 不是真的,走上面。

secretE = not secretB

if secretE:

hero.moveXY(44, 33)

else:

hero.moveXY(44, 15)

hero.moveXY(50, 24)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值