Click here to Skip to main content
15,877,360 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
song = ['always', 'look', 'on', 'the','bright','side','of', 'life']

how should I write a for loop, that it may print

always
look
side
of
life

What I have tried:

I tried break and continue functions. tried randint using numpy
Posted
Updated 12-Aug-22 1:44am
Comments
Sauradipta Chaudhury 12-Aug-22 2:23am    
Can you share the code that you tried?
johng92 12-Aug-22 2:46am    
I found this one:

skipcount = -1
song = ['always', 'look', 'on', 'the','bright','side','of', 'life']
for sing in song: 
    if sing == 'look' and skipcount <= 0: 
        print (sing) 
        skipcount = 3 
    elif skipcount > 0: 
        skipcount = skipcount - 1
        continue 
    elif skipcount == 0:
        print ('a' + sing)
        skipcount = skipcount - 1
    else: 
        print (sing)


this one I found in the internet , But I am not able to understand. from which side it is skip counting , from which side.
Patrice T 12-Aug-22 2:35am    
Show your code for help.
What is the rule to know what to print ?

Quote:
this one I found in the internet , But I am not able to understand. from which side it is skip counting , from which side.

Get rid of this code, it is specially crafted for this specific input and specific skip.

My approach would be :
Python
song = ['always', 'look', 'on', 'the','bright','side','of', 'life']
# list of positions to keep
keep = [0, 1, 4, 5, 6, 7]
for pos in keep:
    print (song[pos])
 
Share this answer
 
v2
Comments
sunnyjohn 18-Oct-22 13:06pm    
Thanks a lot Patrice:) It solved my query.
Python is meant to make things easier, I was making it difficult.
Patrice T 18-Oct-22 13:35pm    
You should accept useful solutions.
It reward authors and close the question as solved.
sunnyjohn 18-Oct-22 13:48pm    
Okay, I will. thanks for precious suggestions.
skipcount = 1
song = ['always', 'look', 'on', 'the','bright','side','of', 'life']
for sing in song: 
    if skipcount != 3 and skipcount != 4: 
        print (sing) 
        skipcount = skipcount + 1
    else: 
        continue


The above logic will work when you have
['always', 'look', 'on', 'the','bright','side','of', 'life']
as your fixed input and you only want to skip the
3rd & 4th
words.

But, if you want to skip a specific set of words that you need to skip from any given array of words then you should follow what @OriginalGriff mentioned.
 
Share this answer
 
Comments
OriginalGriff 12-Aug-22 7:15am    
Um ... Reason for my vote of one:

Did you test it?
What happens when skipcount reaches 3?
The simplest solution is to have a separate collection of "insignificant words":
Python
ignoreThese = ['on', 'the']

And in your loop just check if the current word is included in the "ignorable" collection.
If it is, don't print it. Otherwise, do.

That way, it's more flexible - it can cope with any input string without "special case" coding being required.

Give it a try - Python has the in operator which would help - you'll see what I mean!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900