math.lcm() gives error "Module 'math' has no 'lcm' member"
Question:
I was making a simple calculator:
import math
number1 = input("Enter a number: ")
number2 = input("Enter a number: ")
result = math.lcm(int(number1), int(number2))
print(result)
when I got the error in the title. This works in the shell and even code as simple as
import math
math.lcm(10, 20)
gives me the error.
Answers:
Well, let’s take a look at the documentation for math.lcm
to see why it sometimes exists and sometimes doesn’t exist!
What’s it says? New in version 3.9.
Looks like your code works when run with python 3.9, and doesn’t work when run with python 3.8 or older.
Quick fix, python 3.5 to 3.8
On the other hand, math.gcd
exists since version 3.5. If you need an lcm
function in python 3.5, 3.6, 3.7 or 3.8, you can write it this way:
import math
def lcm(a,b):
return (a * b) // math.gcd(a,b)
Quick fix, python < 3.5
So lcm
is in math
since 3.9, and gcd
is in math
since 3.5. What if your python version is even older than that?
In older versions, gcd
wasn’t in math
, but it was in fractions
. So this should work:
import fractions
def lcm(a,b):
return (a * b) // fractions.gcd(a,b)
Which python version am I using?
Please let me refer you to the kind user who asked that exact question:
- StackOverflow: How do I check what version of Python is running my script?
- Using the python 3.9 interpreter in Visual Studio Code [thanks to user:Jill Cheng for this link]
lcm() can have more than two arguments, like in Python 3.9+:
from math import gcd
#def lcm(a,b):
# return (a * b) // gcd(a, b)
def lcm(*integers):
a = integers[0]
for b in integers[1:]:
a = (a * b) // gcd (a, b)
return a
I was making a simple calculator:
import math
number1 = input("Enter a number: ")
number2 = input("Enter a number: ")
result = math.lcm(int(number1), int(number2))
print(result)
when I got the error in the title. This works in the shell and even code as simple as
import math
math.lcm(10, 20)
gives me the error.
Well, let’s take a look at the documentation for math.lcm
to see why it sometimes exists and sometimes doesn’t exist!
What’s it says? New in version 3.9.
Looks like your code works when run with python 3.9, and doesn’t work when run with python 3.8 or older.
Quick fix, python 3.5 to 3.8
On the other hand, math.gcd
exists since version 3.5. If you need an lcm
function in python 3.5, 3.6, 3.7 or 3.8, you can write it this way:
import math
def lcm(a,b):
return (a * b) // math.gcd(a,b)
Quick fix, python < 3.5
So lcm
is in math
since 3.9, and gcd
is in math
since 3.5. What if your python version is even older than that?
In older versions, gcd
wasn’t in math
, but it was in fractions
. So this should work:
import fractions
def lcm(a,b):
return (a * b) // fractions.gcd(a,b)
Which python version am I using?
Please let me refer you to the kind user who asked that exact question:
- StackOverflow: How do I check what version of Python is running my script?
- Using the python 3.9 interpreter in Visual Studio Code [thanks to user:Jill Cheng for this link]
lcm() can have more than two arguments, like in Python 3.9+:
from math import gcd
#def lcm(a,b):
# return (a * b) // gcd(a, b)
def lcm(*integers):
a = integers[0]
for b in integers[1:]:
a = (a * b) // gcd (a, b)
return a