Not expected output shown | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Not expected output shown

n=8 list=[3,21,5,6,14,8,14,3] #expected output=[3,5,21,6,8,14,3,14] for i in range (0,n-1,1): if list[i]%7==0: list[i],list[i+1]=list[i+1],list[i] i=i+1 print(list)

14th Mar 2023, 3:12 PM
Atul [Inactive]
23 Answers
+ 4
A for...loop uses a fixed range and stepping, but in this case we have to adjust the loop counter <i> sometimes only, not always. That happens when element at index <i> is multiple of 7. A while...loop is more suitable IMHO when we need to adjust loop counter in various ways, maybe even with different stepping values.
14th Mar 2023, 5:53 PM
Ipang
+ 2
Bob Yes you got it right man! While...loop allows us to change loop counter arbitrarily, for...loop in other language also supports loop counter modification; but it seems not the case in Python, cmiiw
15th Mar 2023, 3:22 PM
Ipang
+ 1
As per given expected output it is not clear what you want.
14th Mar 2023, 3:51 PM
A͢J
A͢J - avatar
+ 1
Because the loop counter (index) needs adjustment when an element at the said index is fully divisible by 7. Otherwise I'd be swapping elements at the unintended index next time around.
14th Mar 2023, 5:11 PM
Ipang
+ 1
Thanks a ton Ipang
14th Mar 2023, 5:55 PM
Atul [Inactive]
+ 1
Ok so what I'm seeing is if list[n] is a multiple of seven, have it swap places with list[n+1] and increase the loop counter by 2, otherwise do nothing and increase counter by 1. Do I have it right?
15th Mar 2023, 1:16 AM
Bob
+ 1
Bob That's right! If element[ i ] is multiple of seven then we swap element value with the next one ( element [ i + 1 ] ), and increment <i> because we want to skip element[ i + 1 ] next time around. We rather want to continue checking multiples of 7 again from element[ i + 2 ] because element [ i ] and element [ i + 1 ] had already been swapped, no need for a re-check.
15th Mar 2023, 5:55 AM
Ipang
+ 1
Ok, assuming I have it right, the i+=1 in your if statement is a different i than the iterator. The iterator can't(easily) be modified in the body of a for loop. If you need the iterator to change by a different amount based on a condition, then you have to use a while loop
15th Mar 2023, 12:20 PM
Bob
0
Remember this is a QUESTION and answer section. So what exactly is your question and the issue you are facing?
14th Mar 2023, 3:37 PM
Justice
Justice - avatar
0
Justice I have already mentioned expected output
14th Mar 2023, 3:47 PM
Atul [Inactive]
0
Atul [Inactive] But you don't state the actual output. It's always best to share your code via codebits so that we don't try and guess.
14th Mar 2023, 3:51 PM
Justice
Justice - avatar
0
A͢J Justice just run the above code in code playground and find the difference between the outputs and fix the error
14th Mar 2023, 3:52 PM
Atul [Inactive]
0
Atul [Inactive] Or! Since you are the one asking for help, you can adjust your post to make it easier for others to help instead of making not just me or A͢J, but anyone interested in helping you that sees this question, do the work instead.
14th Mar 2023, 3:54 PM
Justice
Justice - avatar
0
Ok Justice I will make it sure next time
14th Mar 2023, 3:58 PM
Atul [Inactive]
14th Mar 2023, 3:58 PM
Atul [Inactive]
0
Atul [Inactive] Can you explain what you want your algorithm to do exactly (not just the output). Also, how have you tried debugging this?
14th Mar 2023, 4:03 PM
Justice
Justice - avatar
0
The question is swap the next value divisible by 7
14th Mar 2023, 4:05 PM
Atul [Inactive]
0
Atul [Inactive] I would suggest adding some print statements to see what your code is doing. You can put one right before the conditional (if statement) as well as printing out what you presume to be swapping.
14th Mar 2023, 4:09 PM
Justice
Justice - avatar
0
We need to adjust value of <i> when <i> is fully divisible by 7. I think use of while...loop is preferable in this case. We're not supposed to alter a for...loop counter, we use while...loop when we need to. lst=[ 3, 21, 5, 6, 14, 8, 14, 3 ] n = len( lst ) #expected output=[3,5,21,6,8,14,3,14] i = 0 while i < n - 1: if lst[ i ] % 7 == 0: lst[ i ], lst[ i + 1 ] = lst[ i + 1], lst[ i ] i += 1 i += 1 print( lst ) P.S. I would suggest you not to use keywords for variable names, like `list`. You will overwrite `list` definition and confuse yourself in bigger codes/projects when you need to use the original `list` keywords.
14th Mar 2023, 4:36 PM
Ipang
0
Ipang why u have written i+=1 two times ?And why only i+=1 when it is doing the work of skipping the next number?
14th Mar 2023, 5:03 PM
Atul [Inactive]