Reputation: 53
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!
Upvotes: 0
Views: 65
Reputation: 11070
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
Upvotes: 1