#π Use and in an for loop?
56 messages Β· Page 1 of 1 (latest)
@vocal galleon
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
for i in [1,2,3] and [4,5,6]:
print ("1")
Why the and? Why not just make a 6-element list?
And why not use range?
[1,2,3] and [4,5,6] is equivalent to [4,5,6]. You can concat lists using + though: [1, 2, 3] + [4, 5, 6]
Because in my game those are two different objects, I'm not able, or do not know how to combine both without breaking my code ':)
I will try that, thank you!
!e
for i in range(6):
print("1")
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1
002 | 1
003 | 1
004 | 1
005 | 1
006 | 1
!e
# fuck this extremely shitty code
for i in [1, 2, 3] + [4, 5, 6]:
print("1")
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1
002 | 1
003 | 1
004 | 1
005 | 1
006 | 1
So basically, my code is like this:
class X:
def innit(inventory):
self.inventory = [item1, item2]
class Y_:
def innit(inventory):
self.inventory = [item3, item4]
if item3 is either in y.inventory or x.inventory do ABC
that is why I'm trying tpo use:
for item.name in y.inventory or x.inventory:
print ("x")
I hope this is somewhat clear, my code is on my work laptop, so I cannot access that right now.
did you mean to print something from the inventory
cause you're not actually printing anything from the inventory
Yes, so well this is working if I only use the for loop for one object, but once I try to go through both it stops working
zip(*iterables, strict=False)```
Iterate over several iterables in parallel, producing tuples with an item from each one.
Example:
```py
>>> for item in zip([1, 2, 3], ['sugar', 'spice', 'everything nice']):
... print(item)
...
(1, 'sugar')
(2, 'spice')
(3, 'everything nice')
```...
you're not actually printing anything from the inventory
but use zip yeah
Anytime
Oh small question, I just saw that this is specifically for tuples, can you use it for lists aswell? I need to be able to add things to the lists.
:white_check_mark: Your 3.12 eval job has completed with return code 0.
[1, 2, 3]
(1,2,3) being a tuple
Ah I got an idea on how to do this then, thank you again!
No problemo
Are you involving the position of the elements in the list when you print your final results?
Like you want to put the position of the element and the element of the second list?
Why are those two different classes?
Those should be the same class, like Player, then each player would be an instance of this class. (Think car factory: they make cars, the "Car" class is kind of general idea of a car, then each car is produced and is used differently)
Also, init, not innit
Γn that case you should use enumerate()
Also, I don't think zip is a good idea - it means going through both lists simultaneously, while OP specifically said she wanted to search for an item in all inventories...
No those are irrelevant, I basically want to make a function where I say "If ItemX is in either the player inventory or in the loctation inventory then do Y (pick it up for example)"
Tuples are immutable after they're made. You can modify a like (eg append). Otherwise, they're both sequences and you can iterate over them etc as expected.
I wrote this out of my head, sorry for the wrong spelling! ':]
One Class is the player and the other class is the location
I think that you could make a variable in the class that would hold everyone's inventory all together and just check if the itemX is in that variable
Ok. You need to write the condition differently. This:
if itemX in player_inv or location_inv:
means:
if (itemX in player_inv) or (location_inv):
You need:
if itemX in player_inv or itemX in location_inv:
Tried that, but that caused an error aswell
You'd need to show us the error.
You can just do a function with two loops. You want to check if item is in either, you don't need to do anything else with that item in that function, right? So just yes/no (True/False) whether the item is in either, right?
Unable to do that right now, my laptop is at work. But I think the issue was that I tried to implement an error catch, where I said if itemX isn't in player.inventory or location.inventory then print("ERROR") and it printed Error way to often.
When checking if something is equal to one thing or another, you might think that this is possible:
# Incorrect...
if favorite_fruit == 'grapefruit' or 'lemon':
print("That's a weird favorite fruit to have.")
While this makes sense in English, it may not behave the way you would expect. In Python, you should have complete instructions on both sides of the logical operator.
So, if you want to check if something is equal to one thing or another, there are two common ways:
# Like this...
if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon':
print("That's a weird favorite fruit to have.")
# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
print("That's a weird favorite fruit to have.")
As Cameron said
Probably your condition is wrong. For example, in my code above, this:
if (location_inv):
means "if location_inv is not empty". So always true most likely.
Thank you for your help, I'm going to try some of these tips and if for whatever reason this doesn't work I'm comming back with the actual code ':D
Thank you!
Good luck on your journey
In general, in python a collection is "truthy" if it is not empty and "falsey" if it is empty. Applies to lists, tuples, strings etc.
Thank you, I started this at work and honestly it's a lot of fun!
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.