Would you find why am i getting "0" when running this code?

Viewed 43
def multiple_of(n):
    result = 0
    for x in range(0,101):
        x%n == 0
        return(x)
        result +=1
multiple_of(7)
2 Answers

I'm not sure what you're trying to achieve, but if you want multiples of a number up to certain number limit range, following code works.

def multiples_of(num,limit):
    results = []
    for x in range(limit):
        if x%num == 0:
            results.append(x)
    return results

Your code has a lot of mistakes.

1- First mistake is I think you are creating a generator function and in this case you can not use return you need to use yield to be able to make the function return many valuse to deal with in your code. If you used return it will go out the function with return the first value.

2- second mistake you are increasing result variable without use and that variable has no benefits in your code.

And I think you are trying to find the numbers who are equal zero when you devid them on the number that you send into the function

You can write your code like this:


def multiple_of(n):
    for x in range(0,101):
        if x%n == 0:
            yield x

for i in multiple_of(7):
    print(i)

Related