2

I am new to Python. In my module- Utility.py, the function below takes in the age and gender of a person. It returns the fees this person has to pay based on his/her age and gender. I'm not allowed to make any changes to this module.

def get_fees(age, gender):
    if gender == 'M':
        if age >= 50:
            return 300 
        elif age >= 35:
            return 250
        else:
            return 150
    else:
        if age >= 50:
            return 320
        elif age >= 35:
            return 270
        else:
            return 160

I also made a new module, PrintFees.py, where I need to prompt the user for the age and gender and then pass the inputs to the function (get_fees) that I need to call from the Utility module. I also need to print the fees needed.

Here is what I tried:

import Utility
age= int(input("What's your age? " ))
gender = input("What's your gender? [M|F]")
#Print fees 
print("Your fees is $", Utility.get_fees(age,gender))

When I tried to run, this error message appeared.

Traceback (most recent call last):
File "printfees.py", line 7, in <module>
print("Your fees is $", Utility.get_fees(age,gender))
NameError: name 'Utility' is not defined

How do I solve this problem? Thanks.

Vladyslav
  • 1,832
  • 4
  • 17
  • 42
Jane
  • 47
  • 1
  • 6
  • 1
    Those are not classes, they are modules. – Moses Koledoye Sep 01 '17 at 08:36
  • If the import was done I don't see why what you've shown should not work. See, your error says `printfees.py`, you're saying `PrintFees.py`. You've probably done the same with `Utility.py` – Moses Koledoye Sep 01 '17 at 08:38
  • 1
    First of all, your terminology is a bit off. You did not create any classes, you created modules. Module is just the Python lingo for a file with Python code. Now for your problem: I can't reprodcue the error. Something else you did not show us is going on. – timgeb Sep 01 '17 at 08:38
  • @timegeb Sorry. I made a mistake on that. Yes, both modules are in the same directory. – Jane Sep 01 '17 at 08:39
  • your problem is with the indentation of the get_fees function. – Bhawesh Chandola Sep 01 '17 at 08:43
  • 1
    define '__init_.py' in module directory to define directory a python direcetory or package. or use 'from utility import get_fees' – Kallz Sep 01 '17 at 08:45
  • @Kallz Thanks!from utility import get_fees works for me. – Jane Sep 01 '17 at 08:59
  • @Jane Glad to help, if this answer solved your problem please mark it as accepted by clicking the check mark next to the answer. see: 'https://stackoverflow.com/help/someone-answers'? for more information – Kallz Sep 01 '17 at 09:05

3 Answers3

0

First of all you need the Utility modual in the same folder as your code or in the site packages in the python directory. If it is what is the name f the class in the Utility code. I will show a example:

import Utility
Utility.className.functionName(arguments)
Enchant97
  • 300
  • 2
  • 12
0

First of all your directory structure be like this

path/
 |
 |----__init__.py  
 |----Utility.py
 |----PrintFees.py

purpose of __init_.py

In Utility.py

 def get_fees(age, gender):

    if gender == 'M':
        if age >= 50:
            return 300 
        elif age >= 35:
            return 250
        else:
            return 150
    else:
        if age >= 50:
            return 320
        elif age >= 35:
            return 270
        else:
            return 160

In printfees.py

form Utility import get_fees


age= int(input("What's your age? " ))
gender = input("What's your gender? [M|F]")
#Print fees 
print("Your fees is $ %s", str(get_fees(age,gender)))
Kallz
  • 2,856
  • 1
  • 19
  • 38
-1

I guess your problem is with indentation in the get_fees function:

instead of this:

def get_fees(age, gender):

if gender == 'M':
    if age >= 50:
        return 300 
    elif age >= 35:
        return 250
    else:
        return 150
else:
    if age >= 50:
        return 320
    elif age >= 35:
        return 270
    else:
        return 160

do this:

def get_fees(age, gender):

    if gender == 'M':
        if age >= 50:
            return 300 
        elif age >= 35:
            return 250
        else:
            return 150
    else:
        if age >= 50:
            return 320
        elif age >= 35:
            return 270
        else:
            return 160
Bhawesh Chandola
  • 509
  • 5
  • 18