#πŸ”’ Use and in an for loop?

56 messages Β· Page 1 of 1 (latest)

vocal galleon
#

Hey this is a simplification of my code, but I basically want to do this:
I want the loop to give out 1 six times. Is there are way to do this? I'm a beginner and am building a simple object based text adventure and I'm using the for loop to find an item in the players and the locations inventory.
Any help is appreciated!

steep ospreyBOT
#

@vocal galleon

Python help channel opened

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.

vocal galleon
#

for i in [1,2,3] and [4,5,6]:
print ("1")

tepid narwhal
#

Why the and? Why not just make a 6-element list?

And why not use range?

ripe sand
#

[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]

vocal galleon
#

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 ':)

vocal galleon
jovial idol
steep ospreyBOT
jovial idol
steep ospreyBOT
vocal galleon
#

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.

jovial idol
#

cause you're not actually printing anything from the inventory

vocal galleon
#

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

mellow fable
#

You can use zip() function to go through both lists at once

#

!d zip

steep ospreyBOT
#
zip

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')
```...
jovial idol
#

but use zip yeah

vocal galleon
#

Oh that sounds good!

#

Thank you!

mellow fable
#

Anytime

vocal galleon
#

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.

mellow fable
#

You can transform tuples into lists I think

#

!e
print(list((1,2,3)))

steep ospreyBOT
mellow fable
#

(1,2,3) being a tuple

vocal galleon
#

Ah I got an idea on how to do this then, thank you again!

mellow fable
#

No problemo

#

Are you involving the position of the elements in the list when you print your final results?

mellow fable
# steep osprey

Like you want to put the position of the element and the element of the second list?

tepid narwhal
mellow fable
tepid narwhal
#

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...

vocal galleon
mighty quartz
vocal galleon
mellow fable
mighty quartz
vocal galleon
mighty quartz
#

You'd need to show us the error.

tepid narwhal
vocal galleon
#

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.

mellow fable
#

Heh probably due to that or statement

#

!or

steep ospreyBOT
#
The or-gotcha

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.")
mighty quartz
#

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.

vocal galleon
#

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!

mellow fable
#

Good luck on your journey

mighty quartz
#

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.

vocal galleon
#

Thank you, I started this at work and honestly it's a lot of fun!

steep ospreyBOT
#
Python help channel closed

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.