- 4

Can you help me in this code?

FizzBuzz is a well known programming assignment, asked during interviews. The given code solves the FizzBuzz problem and uses the words "Solo" and "Learn" instead of "Fizz" and "Buzz". It takes an input n and outputs the numbers from 1 to n. For each multiple of 3, print "Solo" instead of the number. For each multiple of 5, prints "Learn" instead of the number. For numbers which are multiples of both 3 and 5, output "SoloLearn". You need to change the code to skip the even numbers, so that the logic only applies to odd numbers in the range.

17th Jun 2021, 6:44 PM
zoro
9 Respuestas
+ 7
zoro Your code is approximately correct but there is little mistake to use in continue. You have to skip those numbers which are multiple of 2 so you have to use continue here. n = int(input()) for x in range(1, n): if x % 2 == 0: continue 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)
17th Jun 2021, 6:53 PM
A͢J
A͢J - avatar
+ 1
send with us your attempt on this problem. we would love to guide you to complete it.
17th Jun 2021, 6:46 PM
Prashanth Kumar
Prashanth Kumar - avatar
+ 1
n = int(input()) for x in range(1, n): if x % 2 == 0: continue 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)
22nd Sep 2022, 9:25 AM
ABDUL WAHAB
0
zoro Show your attempts first.
17th Jun 2021, 6:46 PM
A͢J
A͢J - avatar
0
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) continue
17th Jun 2021, 6:49 PM
zoro
0
I have seen 1000 fizzbuzzes, but less than 1 percent check for x%15==0. They all check for x%5==0 and x%3==0. Funny somehow.
17th Jun 2021, 7:48 PM
Oma Falk
Oma Falk - avatar
0
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) continue
2nd Nov 2021, 11:26 AM
Rutha
Rutha - avatar
0
4th Sep 2022, 5:51 AM
Mohamed Desouky
Mohamed Desouky - avatar
0
def fizzbuzz(n): for x in range(1, n, 2): # Iterate over odd numbers from 1 to 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) n = int(input()) fizzbuzz(n) This code works by first defining a function called fizzbuzz(). This function takes an input n and iterates over odd numbers from 1 to n in steps of 2. For each number x, the function checks if it is divisible by 3 and 5. If it is, the function prints "SoloLearn". If it is only divisible by 3, the function prints "Solo". If it is only divisible by 5, the function prints "Learn". Otherwise, the function prints the number x. The main function first prompts the user to enter a number n. It then calls the fizzbuzz() function with the input n. The fizzbuzz() function then prints the output. Here is an example of the output of the code: Enter a number: 15 1 Solo 3 Learn Solo 5 SoloLearn 7 Solo 9 Learn Solo 11 SoloLearn 13 As you can see, the code only prints the odd numbers from 1 to 15. For the even numbers, the code skips them and does not print anything.
22nd Aug 2023, 6:23 AM
Awais Mahboob
Awais Mahboob - avatar