Yakstraction - How to create incremental anouncements?


I had a question on this level. I want to have my adventurer tell me when I have increments of 25 in my self.Gold total value when I pick up coins. Like for example say “I have 25 gold!”, then “I have 50 gold!”, then 75 gold and so on.

I couldn’t think of how to do this because when I put the code in for:

if self.gold >= 25:
    self.say("I have 25 gold!")
if self.gold >= 50:
    self.say("I have 50 gold!")

My warrior kept on repeating self.say(“I have 25 gold!”) (which makes sense because I used >=, but if I use == it might skip the announcement)


I suppose you could say:

if self.gold >= 25 and self.gold < 50:
    self.say("I have 25 gold!")

However, that could prove problematic.


This would still have the warrior repeat “I have 25 gold!” every time it goes through the loop, and ends up slowing me down because I am stopping to say “I have 25 gold!” every time. :frowning:


Well, what if you put each in a separate loop? Like this:

    if self.gold >= 25:
        self.say("I have 25 gold!")
    if self.gold >= 50:
        self.say("I have 50 gold!")

Inefficient, I know, but it works.


while self.gold < 25:
    # Collect coins!
self.say("I have 25 gold!")
while self.gold < 50:
    # Collect more coins!
self.say("I have 50 gold!")

Or, as a third alternative, you could do some math.

The most gold you can have, starting from under 25, then going over 25, would be having 24 gold, then getting a gem, worth 6, which would make your gold 30. So you could say:

if self.gold >= 25 and self.gold <= 30:
    self.say("I have 25 gold!")

you could repeat this process for all your other increments as well. But, then, this is problematic as well.


The problem is that you have them in the wrong order…

75 and 50 are both >= 25
so if you ask that first it will always be true.

you need to ask in the other order

>= 75
>= 50
>= 25

This of course will always say something once you pass 25, and you will have to “be more specific” (as per ChronistGilver) or use a “flag” variable.

>=75 and not blabbed75
    say 75
    blabbed75 = true
>=50 and not blabbed50
    say 50
    blabbed50 = true
>=25 and not blabbed25
    say 25
    blabbed25 = true

Of course, if using the flag variable you could keep the original order.


And with else and the reverse order as proposed Vlevo :

if self.gold >= 50:
    self.say("I have 50 gold!")
else if self.gold >= 25:
   self.say("I have 25 gold!")

(don’t know the exact syntax for else in your langage, but should look like this)

or simpler with only one if, but more tricky :

if self.gold >= 25
    self.say("I have" + (self.gold - self.gold % 25) + " gold!")

But silly question : why you don’t want him to say directly the correct amount of gold ?
Other question : do want him to say that only once when it reached 25, or repeatdly as long as it is between 25 and 49 ?


I will try this when I get back on the computer tomorrow.

To vettax: I want to have my warrior notify me when I have enough gold to buy another decoy! Decoys cost 25 gold so I would want him to let me know when I have enough to buy 1, 2, or 3 without having to look at the total gold in the upper right corner. Also, for extra practice!


Maybe you can use something like : self.say(“Could build” + math.floor(self.gold / 25) + " decoys")

But be carefull, you will face a big disavantage of say method for debug : it takes time ! (it was not the case at the beginning of the game). So each time you speak, you can’t do anything else. :frowning:


Why not just

       self.say("I have 25 gold")``` 
and so on?


Because you can have 24 gold, then collect a silver coin and have 26 gold, skipping the 25 gold entirely.