0

I have this code:

def LCM(minN,maxN):

    count = 1
    for i in range(count,(maxN*count)+1):
        print(minN*count)
        count = count + 1

And if I call it like so: LCM(5,7) its gonna give me the numbers:

5
10
15
20
25
30
35

How could I make the program output (instead of all the numbers) just the last one, in this example: 35

I tried looking it up in the other topics but none were of any help.

3
  • Could you add code of your previous attempts at this? Commented Oct 11, 2019 at 13:24
  • if i==(maxN*count):print((maxN*count))? Commented Oct 11, 2019 at 13:24
  • 1
    Possible duplicate of How do I print only the last value in a for loop (Python)? Commented Oct 11, 2019 at 13:57

5 Answers 5

2

Move the print statement outside the for loop?

def LCM(minN,maxN):
    count = 1
    for i in range(count,(maxN*count)):
        count = count + 1
    print(minN*count)

LCM(5,7)
# 35
Sign up to request clarification or add additional context in comments.

Comments

2

you can simplify your LCM method:

def LCM(minN, maxN):
    print(minN * maxN)

LCM(5,7)

output:

35

Comments

1

You can use a list:

def LCM(minN,maxN):

    count = 1
    results = []
    for i in range(count,(maxN*count)+1):
        results.append(minN*count)
        count = count + 1
    print(results[-1]) # print the last elements of the list.

So, when You call LCM(5, 7), You will get 35.

1 Comment

The solution as stated doesn't print anything
0
def LCM(minN,maxN):
    count = 1
    for i in range(count,(maxN*count)+1):
        count = count + 1
    else:
      print(minN*(count-1))

Comments

0

Let's do some simplification. Here's your original code:

def LCM(minN,maxN):
    count = 1
    for i in range(count,(maxN*count)+1):
        print(minN*count)
        count = count + 1

count could be removed from this:

def LCM(minN,maxN):
    for i in range(1, maxN+1):
        print(minN*i)

Now, you want to print just the last value of this sequence. The last value of i will be maxN:

def LCM(minN,maxN):
    for i in range(1, maxN+1):
        pass
    print(minN * maxN)

Or simply:

def LCM(minN,maxN):
    print(minN * maxN)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.