(the code isn't long, it's just in hide tags because it looks really bad in the middle of normal text. Aaand pretend indenting exists.)
Okay about the join. So here's the line we used that wasn't working:
enemy_fort = [fort for fort in self.forts if fort != team][0]
You at least sort of remember that, right!
The part in brackets is a list comprehension! A list comprehension means instead of making a list and then writing code, like:
my_cats = []
for cat in all_cats_ever:
if cat.owner == me:
my_cats.append(cat)
You stick the code into the brackets!
my_cats = [cat for cat in all_cats_ever if cat.owner == me]
List comprehension syntax is a little bit confusing to learn, but it has some benefits, like: shorter (one line versus four), can be a lot easier to read once you're used to it, and also doesn't pointlessly start an empty list just to append to it. Shorter isn't the goal and you should totally use long code whenever it's easier for you to understand, but sometimes shorter is inherently easier to understand.
Here's a sample of when a list comprehension looks like a mess, too:
data = [(data[i-1].lower().capitalize() + ", " + data).split(", ") for i in range(0, len(data), 3)][1:]
You can tell what the [cat for cats] thing is saying pretty easily, especially when you get used to list comprehensions! But that one up there is confusing and you have to think about it and maybe read more code for context of what's going on before you can edit it. I know what it means because I wrote it recently! (because I was having too much fun experimenting with squishing it all into a list comprehension) But if I forget later it'd take me some energy to work it out. And then once you get what it's doing it's unclear *why* it's doing it. What's the deal with only doing all that data splitting and lowering every three numbers! (that's what that for i in range does) What's so special about every third thing and second thing (I look at third number minus one, too) in the data. Why am I splitting things up! What's data[i-1]. Why do I use [1:] which throws out some arbitrary items from the finished list. Who knows. (It's because I'm parsing a text file for Pokemon attacks and the first couple of lines are a title and an empty, so those are garbage to me. And the other part is because every three lines are attack names! And the line i-1 is above the attack names and is the type like Bug. But who would know that.) Also it's harder to edit because there's a loooot of room for hard-to-notice typos in all those parentheses and brackets. Actually when I was working on that code I introduced a bunch of typos that took way longer to fix than they would have if I hadn't had all that squished into one line.
Anyway, you don't actually need to know that stuff. What you do need to know is that we're doing this with the non-working code:
for player in self.forts:
if player != person_who_typed_the_attack_command:
enemy = player
Since there's only two forts in a game, whichever fort isn't the attacker's fort must be the enemy's fort! Except we stuck it in a list comprehension. So instead of: enemy = "Mewtini" We actually have: enemy = ["Mewtini"]
You're trapped inside a list!!! So ["Mewtini"][0] takes the zeroeth item (and only item) out of the list so that we *do* have enemy = "Mewtini".
But for some reason that caused a problem. So instead of that we have "".join["Mewtini"] which joins all the list items together except there's only one so we just end up with the string "Mewtini". Just. like. it should've. been. already.
*And* we printed out the value before the join statement and it was a right value. It just didn't work before??? I'm sure there's a good explanation and we're just missing some detail but I have no idea what it is. So I gave up and left the join part! I added it in the first place just to be sure we were creating an appropriate string and then it worked.
Also changing the output should be easy! :o We just have to do enemy_fort["HP"] -= damage. Right now we just have it print that without actually ever changing it.
Well it'll be easy when you're able to get online, anyway!!! Was it on Tuesday that he's leaving?
Okay about the join. So here's the line we used that wasn't working:
enemy_fort = [fort for fort in self.forts if fort != team][0]
You at least sort of remember that, right!
The part in brackets is a list comprehension! A list comprehension means instead of making a list and then writing code, like:
my_cats = []
for cat in all_cats_ever:
if cat.owner == me:
my_cats.append(cat)
You stick the code into the brackets!
my_cats = [cat for cat in all_cats_ever if cat.owner == me]
List comprehension syntax is a little bit confusing to learn, but it has some benefits, like: shorter (one line versus four), can be a lot easier to read once you're used to it, and also doesn't pointlessly start an empty list just to append to it. Shorter isn't the goal and you should totally use long code whenever it's easier for you to understand, but sometimes shorter is inherently easier to understand.
Here's a sample of when a list comprehension looks like a mess, too:
data = [(data[i-1].lower().capitalize() + ", " + data).split(", ") for i in range(0, len(data), 3)][1:]
You can tell what the [cat for cats] thing is saying pretty easily, especially when you get used to list comprehensions! But that one up there is confusing and you have to think about it and maybe read more code for context of what's going on before you can edit it. I know what it means because I wrote it recently! (because I was having too much fun experimenting with squishing it all into a list comprehension) But if I forget later it'd take me some energy to work it out. And then once you get what it's doing it's unclear *why* it's doing it. What's the deal with only doing all that data splitting and lowering every three numbers! (that's what that for i in range does) What's so special about every third thing and second thing (I look at third number minus one, too) in the data. Why am I splitting things up! What's data[i-1]. Why do I use [1:] which throws out some arbitrary items from the finished list. Who knows. (It's because I'm parsing a text file for Pokemon attacks and the first couple of lines are a title and an empty, so those are garbage to me. And the other part is because every three lines are attack names! And the line i-1 is above the attack names and is the type like Bug. But who would know that.) Also it's harder to edit because there's a loooot of room for hard-to-notice typos in all those parentheses and brackets. Actually when I was working on that code I introduced a bunch of typos that took way longer to fix than they would have if I hadn't had all that squished into one line.
Anyway, you don't actually need to know that stuff. What you do need to know is that we're doing this with the non-working code:
for player in self.forts:
if player != person_who_typed_the_attack_command:
enemy = player
Since there's only two forts in a game, whichever fort isn't the attacker's fort must be the enemy's fort! Except we stuck it in a list comprehension. So instead of: enemy = "Mewtini" We actually have: enemy = ["Mewtini"]
You're trapped inside a list!!! So ["Mewtini"][0] takes the zeroeth item (and only item) out of the list so that we *do* have enemy = "Mewtini".
But for some reason that caused a problem. So instead of that we have "".join["Mewtini"] which joins all the list items together except there's only one so we just end up with the string "Mewtini". Just. like. it should've. been. already.
*And* we printed out the value before the join statement and it was a right value. It just didn't work before??? I'm sure there's a good explanation and we're just missing some detail but I have no idea what it is. So I gave up and left the join part! I added it in the first place just to be sure we were creating an appropriate string and then it worked.
Also changing the output should be easy! :o We just have to do enemy_fort["HP"] -= damage. Right now we just have it print that without actually ever changing it.
Well it'll be easy when you're able to get online, anyway!!! Was it on Tuesday that he's leaving?