0

I am trying to skip a value (or 2 at a time) from an array in a for loop. Please refer to the code below:

loop = True
product = ['p3','p5','p7','16GB','32GB','1TB','2TB','19in','23in','Mini Tower', 'Midi Tower', '2 ports','4 ports']
while loop:
  for i in product:
    print('Would you like the following component: ',i,)
    input()
    if input == 'y':

If they choose that part, I would like to skip to the next component. Is there any way I can do that in the loop?? Thanks for the help!

4
  • Please, can you continue your example. It seems like its not fully pasted. Commented Feb 10, 2018 at 15:54
  • I was wondering what would come after that last line in order to skip the next value in the array Commented Feb 10, 2018 at 15:55
  • Can you add one expected output? Commented Feb 10, 2018 at 16:51
  • Possible duplicate How to conditionally skip number of iteration steps in a for loop in python? Commented Feb 10, 2018 at 17:42

1 Answer 1

1

You can do this by setting a skip flag, then doing nothing for the next iteration if it is True:

product = ['p3','p5','p7','16GB','32GB','1TB','2TB','19in','23in','Mini Tower', 'Midi Tower', '2 ports','4 ports']

skip = False
for i in product:
  if skip:
    print("Skipping: " + i)
    skip = False
    continue
  if input('Would you like the following component: ' + i) == 'y':
    print("Selected: ", i)
    skip = True

However I'm guessing you are wanting the person to select a processor, memory, screen etc - which is really multiple questions, each with multiple options. In this case, I'd suggest splitting this into a nested list, and stopping after any one selection for each - something like:

product = [['p3','p5','p7'], ['16GB','32GB','1TB','2TB'], ['19in','23in'], ['Mini Tower', 'Midi Tower'], ['2 ports','4 ports']]

for part in product:
  for i in part:
    if input('Would you like the following component: '+i) == 'y':
      print("Selected: ", i)
      break
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.