[Python] While and Loop


#1

At level Decoy Drill,

i’m trying that :

# We are field testing a new battle unit: the decoy.
# Build 4 decoys, then report the total to Naria.
# Each decoy costs 25 gold. Use the Quartz Sense Stone
# to know when you have more than 25 gold with
# Keep a count of decoys you built as you go along.
# Break out of the loop when you have built 4.
decoysBuilt = 0
totalGold = 0

loop:
    while decoysBuilt < 5:
        item = self.findNearest(self.findItems())
        if item:
            position =  item.pos
            x = position.x
            y = position.y
            self.moveXY(x, y)
            totalGold = totalGold + item.value
        if totalGold >= 25:
            self.buildXY("decoy", x, y)
            decoysBuilt = decoysBuilt + 1

self.say("Done building decoys!")
# Go to Naria and say how many decoys you built.
self.moveXY(14, 36)
self.say("J'ai construit 4 decoys")

would you be so kind to explain me the problem with my code ? it is not working after build the first decoy


#2

I’m don’t code python but at first glance if i’m not wrong. It looks like you are using a while loop inside a loop. which is not needed. It’s never going to break out of the while and repeat the loop. In addition you need to put the final step of saying how many you built to an else if none of the other conditions are met, currently it looks like you have it doing that step after you build a single decoy.

decoysBuilt = 0
totalGold = 0

loop:
    if totalGold >= 25:
        self.buildXY("decoy", x, y)
        decoysBuilt = decoysBuilt + 1
    elif decoysBuilt < 5:
        item = self.findNearest(self.findItems())
        if item:
            position = item.pos
            x = position.x
            y = position.y
            self.moveXY(x, y)
            totalGold = totalGold + item.value
    else:
        self.say("Done building decoys!")

        self.moveXY(14, 36)
        self.say("J'ai construit 4 decoys")

#3

it doesn’t work,
i don’t understand why, but, after building the first decay, it stops


#4

So, my solution is (without while …)

# We are field testing a new battle unit: the decoy.
# Build 4 decoys, then report the total to Naria.
# Each decoy costs 25 gold. Use the Quartz Sense Stone
# to know when you have more than 25 gold with
# Keep a count of decoys you built as you go along.
# Break out of the loop when you have built 4.
# We are field testing a new battle unit: the decoy.
# Build 4 decoys, then report the total to Naria.
# Each decoy costs 25 gold. Use the Quartz Sense Stone
# to know when you have more than 25 gold with
# Keep a count of decoys you built as you go along.
# Break out of the loop when you have built 4.
decoysBuilt = 0
totalGold = self.gold

loop:
    if decoysBuilt <= 3:
        item = self.findNearest(self.findItems())
        if item and self.gold < 100:
            position = item.pos
            x = position.x
            y = position.y
            self.moveXY(x, y)
            totalGold = totalGold + item.value
            if self.gold >= 25:
                self.buildXY("decoy", 14, 36)
                decoysBuilt = decoysBuilt + 1
                totalGold = totalGold - 25
    else:
        self.say("Done building decoys!")

        self.moveXY(14, 36)
        self.say("J'ai construit 4 decoys")

however, i’m still not able to properly use “while”


#5

Thats because you don’t need to use while. codecombat has the built in loop construct and using a loop inside will screw it up unless you “break” out of it.


#6

The point is that you’re using totalGold. When you build the first Decoys not subtract the value of Decoy on your totalGold, and therefore, in the next iteration of the loop, you totalGold continues to have 25 or more coins, and then returns to want to build another Decoy ( but do not have enough coins for it!)

The solutions are:

  • Discounting to totalGold prices Decoy once you build it.

  • Use the self.gold variable, which is more effective (how you do in your second code)

  • edit: How well sotonin says. With a loop and break, being decoysBuilt to reach 4 the value of loop termination.

You-while loop is correct, but remember what sotonin told you. If you want to build one decoy, your termination condition would decoysBuilt <1, and so on.


#7

I don’t know if this is because my understanding of english … or of the code, however, i don’t understand nothing to the explaination or the way to code it =)


#8

So I’m a little confused …

Do you understand the code?
Do you understand the while and loop?
Do you know why your code does not work?

I do not understand.


#9

Firstly, i don’t understand the meaning of the slast sentence :

You-while loop is correct, but remember what sotonin told you. If you want to build one decoy, your termination condition would decoysBuilt <1, and so on

Which is probably related to :

I’m don’t code python but at first glance if i’m not wrong. It looks like you are using a while loop inside a loop. which is not needed. It’s never going to break out of the while and repeat the loop. In addition you need to put the final step of saying how many you built to an else if none of the other conditions are met, currently it looks like you have it doing that step after you build a single decoy

that I also don’t understand.

I mean, in my “second” solution, I tell to collect 100, then to build.
However, I would like to tell (as in my first code) collect 25, then, build 1 decoy, then loop to collect 25, and built another one, until that you’ve built 4 decoy. At this point, Stop collect, go to the character, and tell it how many decoy you’ve built, (by using a decoy counter)

That’s why I’m using “while loop”.

There are probably a misunderstanding in code (about while syntax and condition), but also, a misunderstanding about some other things : like the use of some words … for example : "this"
What do you mean by “this” here ? is this a specific word used in python in order to define soimething ? is there a list of this words ? (how are they named ?) Where can i find this list, if it exists ?

Could you be so kind to explain me, the proper use of loop and while loop by an exemple ?

Thank you for your kind collaboration.


#10

Example (with While):

#################
# Build 1 Decoy #
#################

decovsBuilt = 0

while decoysBuilt < 1:
        item = self.findNearest(self.findItems())
        if item:
            position =  item.pos
            x = position.x
            y = position.y
            self.moveXY(x, y)
        if self.gold >= 25:
            self.buildXY("decoy", x, y)
            decoysBuilt = decoysBuilt + 1

self.say("Done building decoys!")
self.moveXY(14, 36)
self.say("J'ai construit " + i + " decoys")

# decoysBuilt = 0 and  this.gold = 0, you hero starts collecting coins. When "self.gold> = 25", enters the second "if". You build one Decoy, and decoysBuilt = 1.
# How "decoysBuilt = 1", the condition of the While ( decoysBuilt < 1 ) is different and terminates the loop.
 # You hero says it has built 1 Decoy.

After this explanation: What condition must have the While to build 4 Decoys?


The while-loop and the loop can be used to solve the same problem. But if you want to use the while-loop, you can look at the example of this response.


It was a mistake of mine. Excuse me. When meant in the previous post “this.gold” meant: “self.gold” but since I’m programming in CodeCombat.com using javascript I got confused (I have already corrected the failure of previous post)


I’ll give an example. Both the “loop” how the “while-loop” end when i = 3:

LOOP

# LOOP: repeats indefinitely. To exit this loop, you must use the sentence "break"

i = 0
loop:
    i = i + 1
    if (i == 3):  # Condition finalization
        break # Exit of the LOOP

self.say ("Value i: " + i)

WHILE-LOOP

# WHILE-LOOP: repeats indefinitely until he reaches the end condition.

i = 0
while (i < 3): # Condition finalization
    i = i + 1 
    # When i = 3, in the next iteration of the while-loop, this will stop because i = 3, and no longer satisfied that i < 3

self.say ("Value i: " + i)

I hope I have solved your doubts.


#11

very clear,
thank you so much