 Python Fizz buzz problem | Sololearn: Learn to code for FREE!

+2

# Python Fizz buzz problem

I wrote the following code to the python Fizz buzz project in the python course and it doesn't work, can anyone tell me what's the bug? n = int(input()) for x in range(1, n+1): if x % 2 != 0: 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) else: continue

0

Soroush Rezaei you are right , honestly to me it sounds like you need to include n as well but I don't have any control on what the sololearn is expecting .

+10

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) Just add "2" in the range.

+7

Soroush Rezaei # Will this work? n = int(input()) for x in range(1, n+1, 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)

+6

for x in range(1, n):

+4

To skip even numbers, you need to code it out. The multiples of 3, multiples of 5, and multiples of 3 and 5 has been coded already. Even number means multiples of 2. I'm not sure if this applies but the API is: range(start, stop, step) start defaults to 0 (inclusive) stop is compulsory (exclusive) step defaults to 1 You might want to change line 3: "range(1, n)" to "range(1, n+1)". So that your input value is included. You can also set the step=2 to increment by 2 to skip even numbers.

+4

Soroush Rezaei There is a problem with the test cases. They did not include "n" as expected results while the question explicitly stated "output 1 to n". To workaround, do "n" instead of "n+1".

+3

Soroush Rezaei for x in range(1, n, 2): range(start, end, step) Iteration starts with 1 and ends with the number with the step 2 so the iteration will look like this 1 3 5 7 ... n The rest is said by sir Lam Wei Li

+2

I have just got the solution of this Problem says : "program should not print even numbers " So you have to skip even numbers by adding steps As range function is (start,stop, step) Solution : for x in range (1, n, 2) You don't have to add n+1

+1

Abhay But the question asks for list of numbers from one to n And hence range(1,n) will list numbers from 1 to n-1 , isn't that wrong?

+1

《 Nicko12 》 Lam Wei Li ♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ First of all, thank you all for responding to my question. But the problem I'm facing here is that the question asks for the list of numbers from 1 to n with iteration of 2 , and hence the range (1,n,2) will give all the odd numbers but n (if n was odd), I'm saying we should write it this way: range (1, n+1, 2) But it won't accept this answer.

+1

for x in range(1, n, 2)

+1

n = int(input()) for x in range(1, n): if x % 2 == 0: continue elif x % 3 == 0 and x % 5 == 0: print("SoloLearn") elif x % 3 == 0: print("Solo") elif x % 5 == 0: print("Learn") else: print(x)

0

Soroush Rezaei Try this one https://code.sololearn.com/c2KXBEB92s8w/?ref=app

0

# Will this work? n = int(input()) for x in range(1, n+1, 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)

0

0

n shouldn't change. Nobody know n is even or odd. Just output odd numbers, solo, learn, and sololearn. So, range start 1 and step is 2, output 1,solo,learn,7,solo,11,13, sololearn,17...and so on.

0

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") elif x % 2 == 0: continue else: print(x) came here for an answer too

-1

Get rid of the n+1 make it n In the test they give 15 n +1 makes the end 16 adding 15 as a new output and printing sololearn Hope this helps

-1

n = int(input()) for x in range(1, 15): if x % 3 == 0 and x % 5 == 0: print("SoloLearn") elif x % 3 == 0 and x % 2 != 0: print("Solo") elif x % 5 == 0 and x % 2 != 0: print("Learn") elif x % 2 == 0: continue else: print(x)