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")
11
  • 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? Commented Oct 17, 2022 at 0:21
  • 1
    Did you mean addIns[i] = addInPrices[i]? Checking for equality without doing anything with that comparison is otherwise pointless. Commented Oct 17, 2022 at 0:21
  • With more information we can likely say what you're doing wrong, and someone can almost certainly provide a better approach. Commented Oct 17, 2022 at 0: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. Commented Oct 17, 2022 at 0:29
  • if you want only correct answer then remove else: ... Commented Oct 17, 2022 at 0:30

3 Answers 3

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")
Sign up to request clarification or add additional context in comments.

Comments

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")

Comments

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!

2 Comments

My question has been closed because it was similar to others. However, I wanted to thank you. Your piece of code gave me the closest 100% answer I was looking for. Thank you.
@Brandonman1 Ah it's no big deal! :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.