0

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)
0

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1
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

6 Comments

Don't know why the downvote but, since your solution works, here's a countervote. Though I have to say I'm not a big fan of import *, I prefer to import only what I need into the current namespace. I'd probably also use import arithmetics as a since I hate typing :-)
@paxdiablo cheers mate, and you are correct. I do prefer to do from module import function over the other. I'll make a note of it.
The first version of your answer was nothing but code. @Torxed
@vaultah Yes, but it does solve the problem, that is not necessarily reason enough for a down vote. If it were bad code without a explanation sure, otherwise it's just not worth the upvote. Stop punishing answers that works but don't promote them if you feel they need an explanation. It's not fair and it doesn't promote the will to solve code issues. Some people might not be qualified to explain WHY but they can show HOW. That's all I can say.
I'm not going to argue. This was discussed many times and the consensus is "we don't need code-only answers". Also: meta.stackexchange.com/a/134096/260312
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.