-1

I'm creating a Fizzbuzz code but there is a problem with the code where the result is like this:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

Here is my code:


word = [3,5]
ans = ["Fizz","Buzz"]

i = 1
j = 0
result = ""

while i <= 20:
    while j <= len(word)-1:
        if i % int(word[j]) == 0 : result += ans[j]
        if result == "" : result = str(i)
        j+=1
    print(result)
    i+=1

The code check on list of the numbers and output the the result or else it will output the number itself.

Franc
  • 31
  • 4
  • 4
    Several issues, but to list a few: `j` is permanently `2` after the first iteration of the outer loop, and `result == ""` will never be true after the first iteration. Also, why `int(word[j])`, using `while` loops instead of far more natural `for` loops, and the bizarre formatting? It seems like you may be learning Python from an untrustworthy source. – Brian61354270 Aug 16 '21 at 19:06

2 Answers2

2

the function logic is weird:

first - you don't need to check the length of word -1 ( it will be 1 every time ),

second - after you understand that j is 2, you will see that you cant check if

if i % int(word[j]) == 0

becose there isnt anything in word in location 2 ,

third - using while loop here insted of for loop is uncommon

you have more errors but instead of listed them try to do it again differently.

answers :

instead of using 2 variables and array and all the things you assigned (and if you still prefer do it with while loop )

i = 1
result = ""

while i <= 20:
   if i%5 == 0 and i%3 == 0 : result="FizzBuzz"
   elif i%5 == 0: result="Fizz"
   elif i%3 == 0: result="Buzz"
   else : result = i
   print(result)
   i+=1

if you want to do it with for loop

for fizzbuzz in range(1,21):
    if fizzbuzz % 3 == 0 and fizzbuzz % 5 == 0:
        print("fizzbuzz")
        continue
    elif fizzbuzz % 3 == 0:
        print("fizz")
        continue
    elif fizzbuzz % 5 == 0:
        print("buzz")
        continue
    print(fizzbuzz)
elaad
  • 21
  • 4
-1

Well never mind, turns out i found out the answer myself:

word = [3,5]
ans = ["Fizz","Buzz"]

i = 1 
while i <= 100:
    result = ""
    j = 0
    while j < len(word):
        if i % int(word[j]) == 0 : result += ans[j]
        j+=1
    if result == "" : result = str(i)
    print(result)
    i+=1

There is 3 errors that I've found Turns out that the j variable is counting continuously so and it couldn't reset so all I have to do is place the variable j in the first while loop.

while i <= 100:
    j = 0

The second issue is similar to j variable so i've place in the first while loop also:

while i <= 100:
    result = ""
    j = 0

The third errors is within this code:

if result == "" : result = str(i)

so all i have to do i declare it outside the 2 loop and there we have the result right here:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz

So here is a lesson I've learn which is : variable acts on where you place it.

Happy coding guys and thank you for your wonderful help and ideas.

Franc
  • 31
  • 4
  • Glad you got it working. If you want to improve your answer, I would suggest learning about for loops in python, type casting and when to use them (and when not to use them), None values, and the range function. The issues you encountered were partly due to using the wrong tools for the job. learn about the tools the language has to offer, and you will have much less errors – Almog-at-Nailo Aug 17 '21 at 10:32
  • Also try using `print()` for debugging and you could find those issues more easily – Almog-at-Nailo Aug 17 '21 at 10:34