Click here to Skip to main content
15,885,920 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im not able to solve this please help

What I have tried:

For i in range(0,n+1):
    if(i%6==0):
        Continue 
        Sum=sum+i
        print(int(sum))
print(sum)
Posted
Updated 10-Aug-22 3:10am

Try
Python
For i in range(0,n+1):
    if(i%6==0):
        Continue 
    Sum=sum+i
    print(int(sum))
print(sum)
 
Share this answer
 
To add to what Patrice has - rightly - said: indentation in Python is significant.
All code indented to the same level forms a single code block whioch only ends when a lesser level on indentation is found,
So this code:
Python
For i in range(0,n+1):
    if(i%6==0):
        Continue 
        Sum=sum+i
        print(int(sum))
print(sum)
Contains two code blocks inside the for loop, one nested inside the other. One code block is the if statement:
Python
For i in range(0,n+1):
    if(i%6==0):
       ...
print(sum)
The other is the content of the if which is only executed if the condition is true, and is executed in its entirety:
Python
Continue
Sum=sum+i
print(int(sum))
Since the first instruction in that code block causes the for loop to immediately carry on with the next iteration ignoring all further code in the loop body, you get no sensible output.

As Partrice says: indent your code to multiple blocks:
Python
For i in range(0,n+1):
    if(i%6==0):
        Continue 
    Sum=sum+i
    print(int(sum))
print(sum)
And it'll do what you want.

Alternatively, reverse the test:
Python
For i in range(0,n+1):
    if(i%6 != 0):
        Sum=sum+i
        print(int(sum))
print(sum)
And you don't need the continue at all - which is more readable!
 
Share this answer
 
for i in range (0,n+1);
if (i%6==0);
continue
sum=sum+i
printf (int(sum))
printf (sum)
 
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