Condition stays false (shine getter)

Hi there, i dont really know why but a statement wich i would guess should be actually true seems to be false or is this my false?

loop:
    coins = self.findItems()
    coinIndex = 0
    
    # Schreibe dies in eine Schleife, die über alle Münzen läuft.
    coin = coins[coinIndex]
    while True:
        # Goldmünzen haben einen Wert von 3.
        if coin.value == 3: #this statement should be true shouldnt it?
            self.say("The code is working but im dumb")
            self.moveXY(coin.x, coin.y)
            coinIndex += 3
            # Sammle nur Goldmünzen.
        else self.say("The statement is false")

Or do i kind of have to create a move to next coin as target thingy because it always targets the same coin wich is not a gold coin resulting that the condition is always false? If yes, how do i do that?

Hello, Profinoob, and welcome. Please format your code according to the FAQ.

while True creates an infinite loop. You want to loop over all the coins, and only add one to coinIndex each time.

No, my problem is that theres no coin.value with the value of 3 and the statement stays false. Im aware of the fact that the while True creates an infinite loop, but i wanted to take care of that later. The problem is that it always uses the else statement

This.

coins is a list, and you want to go through all the elements of that list. In python (and most programming languages) the first element has an index of zero. In order to check all the coins, you have to increase that index until you go through the full list.

example:

| element  | value |
|----------|-------|
| coins[0] |   1   |
| coins[1] |   3   |
| coins[2] |   1   |
|        ...       |
| coins[n] |   2   |

So the code should be something like this:

  • get the list of coins
  • set the index to zero
  • do the following while you go through all the coins:
    • if the coin at the current index is gold:
      • pick up coin
    • (otherwise ignore coin – this is obviously optional)
    • increase the index

You can get the length of a list with the built-in len() function, e.g.: len(my_list)

Increasing the index can be done by simply adding 1 to the variable:

index = index + 1

# or shorter:
index +=1

Remember that in python indentation is important (= that’s how you mark “blocks” of code).

Oh, and you may read through previous topics, as you may find answers there already…

Happy coding!


Please :heart: this post if you found it useful! Thanks

1 Like