0

I'm an amateur programmer, and I have come across an issue with a for statement. So my code works, it prints out what it needs to print. However, the issue lies in the fact that the loop will iterate over the entire list until it finds the correct element. The output looks like this if its the fifth element:

Wrong Answer, Wrong Answer, Wrong Answer, Wrong Answer, Correct Answer

All, I want if for the correct answer to be displayed. Would a flag variable enable this? or do I create a temporary variable to store the correct info in and print that. The help would be awesome, also an explanation of how it works too would be nice as well.

for i in range(len(addIns)):
    addIns[i] == addInPrices[i]
    if addIns[i] == addIn:
        print(addIn,  "Price is",  addInPrices[i] + orderTotal)
        break
    else:
        print("Sorry, we do not carry that")
Jack Taylor
  • 5,588
  • 19
  • 35
  • 2
    Could you elaborate? Do you want only "Wrong Answer" to be printed? Only "Correct Answer" to be printed? Print "Wrong Answer" until first "Correct Answer" is printed? Print "Wrong Answer" and "Correct Answer" for all items in a list? – Tomward Matthias Oct 17 '22 at 00:21
  • 1
    Did you mean `addIns[i] = addInPrices[i]`? Checking for equality without doing anything with that comparison is otherwise pointless. – Chris Oct 17 '22 at 00:21
  • With more information we can likely say what you're doing wrong, and someone can almost certainly provide a better approach. – Chris Oct 17 '22 at 00:24
  • @TomwardMatthias I only want the correct answer to be printed if the input matches one of the elements in the array. Else, I want a separate answer to be printed. – Brandonman1 Oct 17 '22 at 00:29
  • if you want only correct answer then remove `else: ...` – furas Oct 17 '22 at 00:30
  • @Chris I did the comparison because in my head it would tell the computer that array1[] should be connected with the same element as array2[]. Does that make sense? Either way if its useless or not, it seems to have worked. – Brandonman1 Oct 17 '22 at 00:31
  • maybe create minimal working code with example data in `addIns` and `addInPrices` – furas Oct 17 '22 at 00:32
  • or maybe you should change intentations and put `else` in the same column as `for`- and you will have special construction `for/break/else` which will run `else` only if code doesn't use `break` inside `for`-loop – furas Oct 17 '22 at 00:34
  • @kaya3 this should get a better canonical - it's a very common logical problem. Those duplicates wind up at an explanation of the `for/else` construct, which is only one possible way to solve the problem and is often criticized as hard to understand or poorly designed. It's also not great to close a how-to question (or worse, a debugging question, if this one is taken that way) with a canonical explaining a language construct, just on principle. – Karl Knechtel Apr 06 '23 at 19:40
  • @KarlKnechtel I suppose you are right, although for this particular question at least, the code is totally correct *except* for the indentation of the `else` block; reducing that indentation by one level without changing any of the rest of the code solves the problem, though you're right that the OP's mistake probably wasn't that they were trying to write a `for`/`else` and got the indentation wrong. I don't know of a good existing canonical for the generalised problem, but if you know of one or create one yourself, please tag me again so I know of it for future reference. – kaya3 Apr 06 '23 at 21:07
  • (I intend to create one, hopefully within the next month or so.) – Karl Knechtel Apr 07 '23 at 04:58

3 Answers3

1

You want to use a variable found = False initially to keep track of whether the addIn is one of the available addIns.

found = False
for i in range(len(addIns)):
    if addIns[i] == addIn:
        print(addIn,  "Price is",  addInPrices[i] + orderTotal)
        found = True
        break
if not found: 
    print("Sorry, we do not carry that")

Example data:

addIns = ['addIn1','addIn2','addIn3','addIn4','addIn5','addIn6']
addInPrices = [1, 2, 3, 4, 5, 6]
orderTotal = 10
addIn = 'addIn5'

This will print:

addIn5 Price is 15

Another concise way of doing this could be:

try:
    print(addIn, "Price is", addInPrices[addIns.index(addIn)] + orderTotal)
except:
    print("Sorry, we do not carry that")
AboAmmar
  • 5,439
  • 2
  • 13
  • 24
1

A python dictionary will associate each add-in to its price. You can then use that dictionary for lookup.

addInPriceMap = dict(zip(addIns, addInPrices))
if addIn in addInPriceMap:
    print(addIn,  "Price is",  addInPriceMap[addIn] + orderTotal)
else:
    print("Sorry, we do not carry that")
tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

Assuming "Wrong Answer" means you're printing "Sorry, we do not carry that", it makes sense that it is printed only once and not when you're iterating through the for loop.

In your for loop, however:

for i in range(len(addIns)):
    addIns[i] == addInPrices[i]  # This line doesn't seem right, we'll get to that later.
    if addIns[i] == addIn:  # Your code either goes here...
        print(addIn,  "Price is", addInPrices[i] + orderTotal)
        break
    else:  # Or here.
        print("Sorry, we do not carry that")

So it is always printing either the price (if the if branch matches) or the "wrong answer" (Python goes to the else branch).

And you did answer your own question! Flag variables! This is how I might suggest you do it:

foundAddIn = False  # Our flag variable
for i in range(len(addIns)):
    if addIns[i] == addIn:
        print(addIn, "Price is", addInPrices[i] + orderTotal)
        foundAddIn = True  # Set the flag here
        break
    # No else branches are needed. We're checking the flag variable later.

if not foundAddIn:
    print("Sorry, we do not carry that.")

Another way of doing it is by using the builtin index method and the in operator.

if addIn in addIns:  # The index method checks for the index of an element.
    print(addIn, "Price is", addInPrices[addInPrices.index(addIn)] + orderTotal)
else:
    print("Sorry, we do not carry that")

By the way, what is this code doing here: addIns[i] == addInPrices[i]?

Anyhow, hope this helps!

Ramen Dev
  • 35
  • 8