Here is the code I am working with. I'm working with two sets of code. When I combine the code into one program, it works the way it is supposed to. When I attempt to import the "arithemtics" module, it will not define the variables. I have spent many hours trying to figure this out and I fear it's something extremely simple
import arithmetics
def main():
num1 = float(input('Enter a number:'))
num2 = float(input('Enter another number:'))
total(num1,num2)
difference(num1,num2)
product(num1,num2)
quotient(num1,num2)
main()
Here is the "arithmetics"input module
def total(num1,num2):
total = num1 + num2
print(format(total, ',.1f'))
def difference(num1,num2):
difference = num1 % num2
print(format(difference, ',.1f'))
def product(num1,num2):
product = num1 * num2
print(product)
def quotient(num1,num2):
quotient = num1 / num2
print(quotient)
import arithmetics
def main():
num1 = float(input('Enter a number:'))
num2 = float(input('Enter another number:'))
arithmetics.total(num1,num2)
arithmetics.difference(num1,num2)
arithmetics.product(num1,num2)
arithmetics.quotient(num1,num2)
main()
This is because the functions are within the arithmetics
module, they are not globally accessible just by doing import arithmetics
, the functions within arithmetics
needs to be called with the module name followed by the function name like this arithmetics.<function>
.
Or you could do:
from arithmetics import *
This will import all the functions from arithmetics
into the scope of your script as if they were defined within the script itself.
Note that doing a from x import *
(particularly the star in this case) is not the most elegant or efficient way of doing things, if possible try to import only the functions you need from a library (in your case a star will work because you're using all the functions anyway, but other libraries might be big and you might not need all the functions).
To do this you can do:
from arithmetics import total, difference, product, quotient
if you use
import arithmetics
You'll need to qualify the function names like this
arithmetics.total
arithmetics.difference
arithmetics.product
arithmetics.quotient
Alternatively
from arithmetics import total, difference, product, quotient
Then the names can be used normally in your module
The 3rd alternative
from arithmetics import *
Is discouraged as it's not explicit and could cause mysterious breakage when someone adds some more names to arithmetics
that can cause conflicts
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.