Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Python noob: how to import math modual
New Posts  All Forums:Forum Nav:

Python noob: how to import math modual

post #1 of 7
Thread Starter 
I am building a program for the quadratic formula but it won't execute the sqrt command. I assume that I need the math modual but don't know how to import it. Please help.
last gen
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel I7 930 Asus P6x58d-p XFX Radeon 6870 Double Dissipation corsair xms3 
Hard DriveCoolingOSKeyboard
OCZ agility 3 Coolermaster Hyper 212+ Win 7 pro 64 bit cherry mx 8100  
PowerCaseMouse
kingwin 750w open air razer naga 2011 
CPUMotherboardRAMOS
core 2 duo apple logic board 4 gb mac osX 
  hide details  
Reply
last gen
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel I7 930 Asus P6x58d-p XFX Radeon 6870 Double Dissipation corsair xms3 
Hard DriveCoolingOSKeyboard
OCZ agility 3 Coolermaster Hyper 212+ Win 7 pro 64 bit cherry mx 8100  
PowerCaseMouse
kingwin 750w open air razer naga 2011 
CPUMotherboardRAMOS
core 2 duo apple logic board 4 gb mac osX 
  hide details  
Reply
post #2 of 7
For importing the "math" module in Python or any other module for that matter, all you have to include in your code before you wish to execute one of its functions is,
Code:
import <module-name>

In this case,
Code:
import math
 
Desktop
(13 items)
 
MacBook Pro 13"
(6 items)
 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
CPUGraphicsRAMHard Drive
Intel i5 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Lion 13.3" @ 1280 x 800 
  hide details  
Reply
 
Desktop
(13 items)
 
MacBook Pro 13"
(6 items)
 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
CPUGraphicsRAMHard Drive
Intel i5 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Lion 13.3" @ 1280 x 800 
  hide details  
Reply
post #3 of 7
Thread Starter 
I put import math at the top of my program yet it still only says sqrt is not defined.
last gen
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel I7 930 Asus P6x58d-p XFX Radeon 6870 Double Dissipation corsair xms3 
Hard DriveCoolingOSKeyboard
OCZ agility 3 Coolermaster Hyper 212+ Win 7 pro 64 bit cherry mx 8100  
PowerCaseMouse
kingwin 750w open air razer naga 2011 
CPUMotherboardRAMOS
core 2 duo apple logic board 4 gb mac osX 
  hide details  
Reply
last gen
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel I7 930 Asus P6x58d-p XFX Radeon 6870 Double Dissipation corsair xms3 
Hard DriveCoolingOSKeyboard
OCZ agility 3 Coolermaster Hyper 212+ Win 7 pro 64 bit cherry mx 8100  
PowerCaseMouse
kingwin 750w open air razer naga 2011 
CPUMotherboardRAMOS
core 2 duo apple logic board 4 gb mac osX 
  hide details  
Reply
post #4 of 7
You may not be using the correct syntax to use the sqrt function. If you want to find the square root of 4, you should have in your code,
Code:
math.sqrt(4)
.
 
Desktop
(13 items)
 
MacBook Pro 13"
(6 items)
 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
CPUGraphicsRAMHard Drive
Intel i5 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Lion 13.3" @ 1280 x 800 
  hide details  
Reply
 
Desktop
(13 items)
 
MacBook Pro 13"
(6 items)
 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
CPUGraphicsRAMHard Drive
Intel i5 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Lion 13.3" @ 1280 x 800 
  hide details  
Reply
post #5 of 7
Quote:
Originally Posted by nathanak21 View Post

I put import math at the top of my program yet it still only says sqrt is not defined.
You have to write the library in front of the function name:
Code:
import math

math.sqrt(25)

EDIT:
Dang ninjas
post #6 of 7
Thread Starter 
Ohhhhh! Ok I get it now! Thanks guys. Rep+ for you two
last gen
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel I7 930 Asus P6x58d-p XFX Radeon 6870 Double Dissipation corsair xms3 
Hard DriveCoolingOSKeyboard
OCZ agility 3 Coolermaster Hyper 212+ Win 7 pro 64 bit cherry mx 8100  
PowerCaseMouse
kingwin 750w open air razer naga 2011 
CPUMotherboardRAMOS
core 2 duo apple logic board 4 gb mac osX 
  hide details  
Reply
last gen
(11 items)
 
   
CPUMotherboardGraphicsRAM
Intel I7 930 Asus P6x58d-p XFX Radeon 6870 Double Dissipation corsair xms3 
Hard DriveCoolingOSKeyboard
OCZ agility 3 Coolermaster Hyper 212+ Win 7 pro 64 bit cherry mx 8100  
PowerCaseMouse
kingwin 750w open air razer naga 2011 
CPUMotherboardRAMOS
core 2 duo apple logic board 4 gb mac osX 
  hide details  
Reply
post #7 of 7
Alternatively if you wanted to do just
Code:
sqrt(25)

You could do it this way:
Code:
from math import sqrt

sqrt(25)

But ONLY sqrt will be imported and no other function.
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Python noob: how to import math modual