Can anybody tell me other method for this code. | Sololearn: Learn to code for FREE!

+1

Can anybody tell me other method for this code.

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 Answer

+2

You can also use continue statement to skip even numbers. for x in range(1, n): if x % 2 == 0: continue elif.....

+1

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.

0

https://www.sololearn.com/Discuss/2688138/sololearn-fizzbuzz-problem-solved

0

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: