Home python for loop with if statement not returning items
Reply: 0

python for loop with if statement not returning items

user9980 Published in September 20, 2018, 10:22 pm

I am trying to learn python through some basic exercises with my own online store. I have a list of parts that are in-transit to us that we have already ordered, and I have a list of parts that we are currently out of stock of. I want to be able to send a list to the supplier of what we need - but I do not want to create duplicate orders as a result of the fact that the parts on order, are listed as out of stock.

I put together this basic program that looks through the list of items that are out of stock and only prints the item if it is present in the outofstock list but not present in the onorder list, so that if it is on order we do not order it again. However, it outputs nothing.

onorder = ["A1417", "A1322", "ISL6259", "LP8545B1SQ", "PM6640", "SLG3NB148V", "PD4HDMIREG", "338S1201", "SN2400B0", "AD7149", "J3801", "J4502", "IPRO97B"]
outofstock = ["ISL6259", "LY-UVH900", "triwing", "banana-to-alligator", "LP8548B1SQ", "EDP-J9000-30-PIN-IPEX", "J3801", "LT3470", "PM6640", "SN2400B0", "IPRO97B", "SLG3NB148V", "SN2400AB0", "usbammeter", "821-00814-A", "J5713", "343S0645", "PMCM4401VPE", "J4502", "PMD9645", "J9600", "J2401", "AD7149", "593-1604", "821-1722", "LM3534TMX", "U4001"]

for part in onorder:
    if (part in onorder) == False and (part in outofstock) == True:
    print (part)

It doesn't print anything, even though there are entries in outofstock that are not in onorder.

If I try this outside of a loop, it works and prints every part in the onorder list.

for part in onorder:
    print (part)

If I try this outside a loop, it also works and prints triwing, since it is true that triwing is in the outofstock list.

if ('triwing' in outofstock) == True:
    print ("triwing")

However, the program in the for loop returns nothing. What am I missing?

share|improve this question
  • 1
    Why not if part not in order and part in outofstock:? – roganjosh Feb 13 at 21:03
  • Indent print (part) by four spaces. – Monkey Supersonic Feb 13 at 21:04
  • 3
    for part in onorder guarantees that part in onorder is always true. – chepner Feb 13 at 21:06
  • I tried if (part not in onorder) and (part in outofstock):, same result. is print (part) not indented in the above example? I will check out what you had to say on duplicate of in operator. Thank you very much! – cfinspan Feb 13 at 21:09
  • Yep, there's two issues. if part not in onorder and part in outofstock: would be the correct approach, if you weren't actually iterating through onorder in the first place meaning that the first condition can never be True. – roganjosh Feb 13 at 21:12

3 Answers 3

active oldest votes
up vote 3 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303908 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO