0
votes

I recently downloaded Twilio quest and I love it! However, I'm stuck at one of the entry-level Python challenges. It should be fairly easy to solve, but somehow I can not seem to work out the error here. Can anyone look through my code and find the obvious mistake which I clearly cannot?

import sys

inputs = sys.argv
inputs.pop(0)



for i in inputs:
    print(i)
    n = int(i) 
    for x in range(1,n+1):
        if x % 3 == 0 and x % 5 == 0:
            print("fizzbuzz")
        elif x % 3 == 0:
            print("fizz")
        elif x % 5 == 0:
            print("buzz")
        else:
            print(x)

The challenge is to solve the fizzbuzz challenge, with multiple numbers as input. Error yields: "We passed your script a number that was divisible by both 3 and 5, and expected you to print fizzbuzz, but instead you printed -3000.

So the input was -3000, and should pass by my first test, as it is indeed divisible by both 3 and 5. I can not seem to work out why the input -3000 would jump to the "else"-part of my for-loop.

2
dont print i before your loop, you are printing the input value. the validation script wont be expecting that - Chris Doyle
Even though I agree with the suggested change, it only swapped the error with a different one (tried this change earlier): "We passed your script a number that was divisible by both 3 and 5, and expected you to print fizzbuzz, but instead you printed 1." I do not understand why it does not print "fizzbuzz" here. Thanks. - Havard Kleven
Well it should not print fizzbuzz for the number 1... - timgeb
As I thought.. Then I think I might report a bug to the Twilio team. 1 would never be divisble by neither 3 or 5.. I do not understand why it would reject the answer 1 here. But I think this might be a bug with the Twilio-program. - Havard Kleven

2 Answers

3
votes

If the input is just a number, there is no need of the for loop. The logic is correct. One fix can be:

import sys

inputs = int(sys.argv[-1])
x=inputs
if x % 3 == 0 and x % 5 == 0:
    print("fizzbuzz")
elif x % 3 == 0:
    print("fizz")
elif x % 5 == 0:
    print("buzz")
else:
    print(x)

for a list of int in input:

import sys

inputs = [int(x) for x in sys.argv[1:]]
for x in inputs:
    if x % 3 == 0 and x % 5 == 0:
        print("fizzbuzz")
    elif x % 3 == 0:
        print("fizz")
    elif x % 5 == 0:
        print("buzz")
    else:
        print(x)
0
votes

Does the question ask you to iterate over each number from 1 to N for every input or does it ask you to only check for a single Number for an input?

If it's only a single element then I believe the following code will suffice:

import sys

inputs = sys.argv
inputs.pop(0)
for i in inputs:
    # print(i)
    n = int(i) 
    if n % 3 == 0 and n % 5 == 0:
        print("fizzbuzz")
    elif n % 3 == 0:
        print("fizz")
    elif n % 5 == 0:
        print("buzz")
    else:
        print(n)