finding multiples of a number in Python

后端 未结 7 1017
甜味超标
甜味超标 2021-02-07 10:48

I\'m trying to write a code that lets me find the first few multiples of a number. This is one of my attempts:

def printMultiples(n, m):
for m in (n,m):
    prin         


        
相关标签:
7条回答
  • 2021-02-07 11:30

    If you're trying to find the first count multiples of m, something like this would work:

    def multiples(m, count):
        for i in range(count):
            print(i*m)
    

    Alternatively, you could do this with range:

    def multiples(m, count):
        for i in range(0,count*m,m):
            print(i)
    

    Note that both of these start the multiples at 0 - if you wanted to instead start at m, you'd need to offset it by that much:

    range(m,(count+1)*m,m)
    
    0 讨论(0)
提交回复
热议问题