Reputation: 325
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.
Upvotes: 0
Views: 1878
Reputation: 62073
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)
Upvotes: 0
Reputation: 17322
you can simplify your LCM method:
def LCM(minN, maxN):
print(minN * maxN)
LCM(5,7)
output:
35
Upvotes: 2
Reputation: 347
def LCM(minN,maxN):
count = 1
for i in range(count,(maxN*count)+1):
count = count + 1
else:
print(minN*(count-1))
Upvotes: 0
Reputation: 1065
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.
Upvotes: 1
Reputation: 88236
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
Upvotes: 2