宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

「网易官方」极客战记(codecombat)攻略-沙漠-许愿井-wishing-well-风君子博客
点击图片进入关卡)

从许愿井中获得准确数目的金币。

简介

你需要刚好 104 金钱。

使用 sumCoinValues) 函数得到金币总价值。

如果不够,说 “Non satis” .

如果太多,说 “Nimis” .

如果的确是 104 金钱,那就开始收集。

提示: 看一下 sumCoinValues) 的代码,看看它怎么做到的!

默认代码

# 你需要104的金钱,不多也不少。
less = “Nimis”
more = “Non satis”
requiredGold = 104
# 此函数计算所有的硬币值的总和。
def sumCoinValuescoins):
    coinIndex = 0
    totalValue = 0
    # 遍历所有的金币。
    while coinIndex < lencoins):
        totalValue += coins[coinIndex].value
        coinIndex += 1
    return totalValue
def collectAllCoins):
    item = hero.findNearesthero.findItems))
    while item:
        hero.moveXYitem.pos.x, item.pos.y)
        item = hero.findNearesthero.findItems))
while True:
    items = hero.findItems)
    # 获得硬币的总值
    goldAmount = sumCoinValuesitems)
    # 如果有金币,那么金币数目 goldAmount) 不会是零
    if goldAmount != 0:
        # 如果goldAmount 小于requiredGold。

 

            # 那就说“Non satis”

 

        # 如果goldAmount 大于requiredGold。

 

            # 那么说出“Nimis”。

 

        # 如果 “goldAmount” 等于 “requiredGold”

 

            # 如果有刚好 104 金币,就全部收集。

 

        pass

概览

史上最简单的 求和函数 就是将数组内的数字相加:

def sumNumbersarray):
    index = 0
    total = 0
    while index < lenarray):
        total += array[index]
        index += 1

我们遍历数组元素,并加到 total 中。

在这关。我们有一个对象(金币)数组,所以我们要把每个金币的 value 属性加起来。

许愿井解法

# 你需要104的金钱,不多也不少。
less = “Nimis”
more = “Non satis”
requiredGold = 104
# 此函数计算所有的硬币值的总和。
def sumCoinValuescoins):
    coinIndex = 0
    totalValue = 0
    # 遍历所有的金币。
    while coinIndex < lencoins):
        totalValue += coins[coinIndex].value
        coinIndex += 1
    return totalValue
def collectAllCoins):
    item = hero.findNearesthero.findItems))
    while item:
        hero.moveXYitem.pos.x, item.pos.y)
        item = hero.findNearesthero.findItems))
while True:
    items = hero.findItems)
    # 获得硬币的总值
    goldAmount = sumCoinValuesitems)
    # 如果有金币,那么金币数目 goldAmount) 不会是零
    if goldAmount != 0:
        # 如果goldAmount 小于requiredGold。
        if goldAmount < requiredGold:
            # 那就说“Non satis”
            hero.saymore)
        # 如果goldAmount 大于requiredGold。
        if goldAmount > requiredGold:
            # 那么说出“Nimis”。
            hero.sayless)
        # 如果 “goldAmount” 等于 “requiredGold”
        if goldAmount == requiredGold:
            # 如果有刚好 104 金币,就全部收集。
            collectAllCoins)
 
本攻略发于极客战记官方教学栏目,原文地址为:
https://codecombat.163.com/news/jikezhanji-shamoxuyuanjing
极客战记——学编程,用玩的