Question->’’’n = int(input()) for x in range(1, n): if x % 3 == 0 and x % 5 == 0: print("SoloLearn") elif x % 3 == 0: print("Solo") elif x % 5 == 0: print("Learn") else : print(x)’’’ My answer-> n = int(input()) for x in range(1, n, 2): if x % 3 == 0 and x % 5 == 0: print("SoloLearn") elif x % 3 == 0: print("Solo") elif x % 5 == 0: print("Learn") else : print(x) I just changed interval of range by 2.
1/17/2022 10:30:06 AM
KINGFISHER[Less Active]....4 Answers
New AnswerYou can also use continue statement to skip even numbers. for x in range(1, n): if x % 2 == 0: continue elif.....
When the step is 1 (default): 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 When the step is 2: 1, 3, 5, 7, 9, 11 As you can see the number 10 was skipped and it is dividable by 5 so the code should output 'Learn'. When the step is eqal to two, you skip some numbers that should be checked.
Kunal B the code looks very good. You could reduce the logic just a little. if x % 3 == 0 and x % 5 == 0: Reduces to the equivalent: if x % (3*5) == 0:
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message