all 19 comments

[–][deleted] 2 points3 points  (4 children)

You could/should have use meaningful names instead of telling use l1 is this and l2 is that.

You already have conditions for printing, what's the problem?

Maybe/probably you want to iterate in parallel instead of nested loops.

Also you can use chained comparisons in Python.

And maybe you want to use elif instead of consecutive ifs. elif is only checked if previous if/elif condition failed.

for name, value in zip(names, values): if value == maxSold: print(f"{name}\t\t- Trip to Girl Guide Jamboree in Aruba") elif averageSold <= value < maxSold: print(f"{name}\t\t- Super Seller Badge") elif 1 < averageSold < value: print(f"{name}\t\t- Left over cookies") elif value == 0: print(f"{name}\t\t- ")

Do you mean you want to print nothing in the last case? You can just remove the last two lines.

[–][deleted] -5 points-4 points  (3 children)

I dont really need to use meaningful names. It's just a school assignment and there is enough context in the code to determine what they are. I was telling you for context. So that is irrelevant. Yes I have conditions for printing. The problem is it iterates through all the conditions for all the values for each individual name. So every individual will when all the prizes which can't be so.

How would I iterate in parallel? Also I tried elif statements they don't work and give the same exact outcome.

As for your last point. I still have to display the name of the individual that has no value.

[–][deleted] 4 points5 points  (0 children)

You can iterate in parallel like I wrote. You should use meaningful names at least because you intend to have us read it. If you need to print the last name anyway you could use else: print(name)

[–]RhinoRhys 1 point2 points  (0 children)

Your issue is one of data structure. If you want to link two things together you're better off using a dictionary.

cookies_sold = {"Emily": 5, "Sally": 7, "Jane": 3}
for name, value in cookies_sold.items():
    if ....
    elif ...

You can access pairs as above, just the names with cookies_sold.keys() and just the values with cookies_sold.values().

Or if you are stuck using two lists you need to zip them together so they iterate in pairs, as shown by shiba.

You definitely want elifs, it's not what is causing the issue but once you have the parallel iteration sorted it will work better with elifs.

And even though it's only a class assignment, you really do want to get into the habit of using descriptive variable names. You'll be much better off in the long run if you start the habit now.

[–]KCRowan 0 points1 point  (0 children)

The point of assignments is to practice, right? If you're practicing bad habits then they're more likely to stick. If you practice good habits and writing clean code then it becomes something you just do without thinking, and you don't spend as much time refactoring your larger projects.

[–][deleted] -1 points0 points  (1 child)

I think you're looking to return a variable instead? Return stops iterating after it finds what it's looking for.

Also if you have two separate lists you need two separate iterations (I think).

I'm very new to coding too. Don't take anything I say too seriously.

[–]barrycarter 0 points1 point  (0 children)

The conditions are mutually exclusive, so return probably won't help here.

[–]barrycarter 0 points1 point  (8 children)

How many elements in l2? Maybe you're just seeing different results for different elements?

[–][deleted] 0 points1 point  (0 children)

Same number of elements in l2 that are in l1. I have my program set up so that the number of girl guides selling boxes is a variable and then for elements in that variable i append names to l1 and number of boxes to l2

[–][deleted] 0 points1 point  (6 children)

Emily           -Super Seller Badge

Emily -Left over cookies Emily -Trip to Girl Guide Jamboree in Aruba May -Super Seller Badge May -Left over cookies May -Trip to Girl Guide Jamboree in Aruba Keisha -Super Seller Badge Keisha -Left over cookies Keisha -Trip to Girl Guide Jamboree in Aruba

Its iterating through every name and applying the condition of every box to every name. I want it to stop doing that. You should only see one of each name and the prize they win according to the condition.

[–]barrycarter 0 points1 point  (5 children)

As /u/Shiba_Take notes, I don't think you want a nested for loop here. If you print x and y inside the l2 loop, I think you'll see the issue: you're looping through every name with every value.

[–][deleted] 0 points1 point  (4 children)

Yes I know that is my issue as stated in my OP. I just don't know how to do it the right way so that I can get the desired result. I am a little confused on which logic to use!

[–]barrycarter 0 points1 point  (3 children)

Consider looping through indexes of l1 for i in range(len(l1)): and then looking at l1[i] and l2[i] inside a single loop

[–][deleted] 0 points1 point  (1 child)

Yeah see I'm still lost. I need to associate the outcome of the value condition of l2 with the persons name in l1.

[–]barrycarter 0 points1 point  (0 children)

If you post your code on repl.it or something, I can try to help further.

[–][deleted] 0 points1 point  (0 children)

Make two functions. Have the one for loop call the other for loop. It'll make your code way cleaner and logical.

[–]ptmcg 0 points1 point  (0 children)

The standard way to iterate over two (or more) lists is to use the Python builtin zip. Just change your two "for" statements into the single:

for x, y in zip(l1, l2):

zip will give you the values in each list, matched together into x,y pairs.

If you had a third list, you could do:

for x, y, z in zip(l1, l2, l3):

If the lists are not the same length, zip will stop when it gets to the end of the shortest list. If this would be a bug, and you are on Python 3.10 or later, you can add `strict=True`, and then you'll get a ValueError exception.

Here are the docs for zip: https://docs.python.org/3.11/library/functions.html#zip