I want to work with logarithms, but my math module doesn't work like it is supposed to.
Here's my code:
import math
n= int(2)
x = n**2
y = 2*n** log(3,2) +1
while float(x) < float(y):
n += 1
print(n)
It prints the following Error:
Traceback (most recent call last):
File "C:\Users\User\Desktop\python\exp102.py", line 72, in <module>
y = 2*n** log(3,2) +1
NameError: name 'log' is not defined
Can you help me put the math module work perfectly, if that's the problem?
If you want to use a function from a package, you have several options to do that. The two most use:
from math import log
This will make log available in the namespace and you can use it directly.
import math
This will make math available and you can access the function as math.log().
Try to import log module with below statement:
from math import log.
Related
I'm making a calculator app. I am using Java and Jython. I wanted to use the SymPy module but after calling the code below I get an error that there is no such module. Any advice?
try (PythonInterpreter interpreter = new PythonInterpreter()) {
interpreter.exec("""
from sympy import cos, sin, pi
def f(x):
\treturn\040""" + wartoscCalki.getText() +
"""
\ndef simpson(a, b, n):
\tx1=0.0
\tx2=0.0
\tx0=f(a)+f(b)
\th=(b-a)/n
\twynik=0.0
\tfor i in range(1,n):
\t\tx=(a+(i*h))
\t\tif i%2==0:
\t\t\tx2+=f(x)
\t\telse:
\t\t\tx1+=f(x)
\twynik=h*(x0+2*x2+4*x1)/3
\treturn wynik
c=float(simpson(""" + granicaDolna.getText() + """
,""" + granicaGorna.getText() + """
,""" + liczbaPodprzedzialow.getText() + """
))"""
);
}
Caused by: Traceback (most recent call last):
File "", line 2, in module
ImportError: No module named sympy
sympy is not a jython module. What you need is this:
from Java.lang import Math
Once imported, you can use Math.sin(), Math.cos(), and Math.PI to perform the above listed calculations.
A complete list of Java.lang.Math methods can be found here:
Oracle Documentation
Problem
I would like to import a script containing many functions and then run them, so that I can use the function. I may have misunderstood the purpose of import. I am working in Jupyter.
Reprex
#Create the script in a local folder
%%writefile test.py
c = 500
def addup(a,b,c):
return a*b + (c)
#import the file and use it
import test
addup(1,5,c)
#Error message
---------------------------------------------------------------------------
# NameError Traceback (most recent call last)
# <ipython-input-1-71cb0c70c39d> in <module>
# 1 import test
# ----> 2 addup(1,5,c)
# NameError: name 'addup' is not defined
Any help appreciated.
You have not called the function! You need a dot . to call a function from a module.
This is the correct syntax:
import test
result = test.addup(1,5,c)
Import a specific function:
from test import addup
addup(1,5,c)
Importing all of the module's functions:
from test import *
addup(1,5,c) # this way you can use any function from test.py without the need to put a dot
I'm trying to import statistics module in python. It's giving me an error message when i execute the program.
Here is my code:
from statistics import mean
import numpy as np
import matplotlib.pyplot as plt
xs = np.array([1,2,3,4,5,6,7,8])
ys = np.array([2,8,5,0,5,7,3,6])
def best_fit_line(xs ,ys):
m = ( ((mean(xs)* mean(ys))- mean(xs*ys)) /
(mean(xs)*mean(xs))-(mean(xs*xs)))
return m
m = best_fit_line(xs,ys)
The error message:
Traceback (most recent call last):
File "/home/kudzai/Documents/Machine Learning/LinearRegAlg.py", line 1, in <module>
from statistics import mean
ImportError: No module named statistics
The statistics module was added in Python 3.4. Perhaps you're using an older Python version.
If you can't upgrade for whatever reason, you can also just use numpy's mean function: np.mean(xs) etc. For numpy arrays, it's probably faster too.
I am a newbie to python. Below is my module
mymath.py
pi = 3.142
def circle(radius):
return pi * radius * radius
In terminal, I run it following way:
>>import mymath
>>mymath.pi
>>3.142
When I change pi to a local variable and reload(mymath) and do import mymath, still I get value of mymath.pi as 3.142. However the result of mymath.circle(radius) does reflect the change in result.
def circle(radius):
pi = 3
return pi * radius * radius
>>import imp
>>imp.reload(mymath)
>>import mymath
>>mymath.pi
>>3.142
>>circle(3)
>>27
Can anyone tell me what might be the issue?
From the docs for imp.reload():
When a module is reloaded, its dictionary (containing the module’s global variables) is retained. Redefinitions of names will override the old definitions, so this is generally not a problem. If the new version of a module does not define a name that was defined by the old version, the old definition remains.
So when you do imp.reload(mymath), even though pi no longer exists as a global name in the module's code the old definition remains as a part of the updated module.
If you really want to start from scratch, use the following method:
import sys
del sys.modules['mymath']
import mymath
For example:
>>> import os
>>> os.system("echo 'pi = 3.142' > mymath.py")
0
>>> import mymath
>>> mymath.pi
3.142
>>> os.system("echo 'pass' > mymath.py")
0
>>> import sys
>>> del sys.modules['mymath']
>>> import mymath
>>> mymath.pi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'pi'
In src/webprofiles/__init__.py I have
def match(string)
Now how can I make a call to this match from `src/python.py, which contains
from webprofiles import *
for x in text
a= webprofiles.match(x)
Its giving me an error
NameError: global name 'webprofiles' is not defined
When you use from import form, you must call function without module prefix.
just call the functions and attributes via their names.
from webprofiles import *
for x in text:
a= match(x)
but i suggest to DO NOT use wildcard('*') imports.
use this instead:
from webprofiles import match
for x in text:
a= match(x)
The syntax from x impoort * means that everything will be imported, in effect, into the global namespace. What you want is either import webprofiles followed by webprofiles.match or from webprofiles import * followed by a call to plain match
Just import webprofiles, not *:
import webprofiles
for x in text
a = webprofiles.match(x)
What you have there seems to me 2 files and you want to run a file which imports the methods contained in an other file:
import /webprofiles/init
init.match(x)
after modifiying your question:
import /webprofiles/__init__
__init__.match(x)
btw when you import something:
import my_file #(file.py)
my_file.quick_sort(x)
^^^^^^ you have to call myfile as you call normally an object
from my_file import *
#that is read as from my_file import everything
#so now you can use the method quick_sort() without calling my_file
quick_sort(x)