Darren Macis
Darren Macis

Reputation: 77

Calling functions within functions

I am trying to call and return a function made up of three other functions, any pointers?

def ForeName():
    forename = raw_input("Please enter you're Forename: ")
    return forename


def MiddleName():
    middle_name = raw_input("Pleaase enter you're Middle name: ")
    return middle_name

def SurName():
    surname = raw_input("Please eneter you're Surname: ")
    return surname

def UserName():
    result = UserName()
    print "result %s" % (result)

UserName()

Upvotes: 0

Views: 59

Answers (2)

Brian
Brian

Reputation: 1025

There is a lot of boilerplate code. You could write something that accepts the type of name you are looking for from the prompt.

def get_name(name_type):
    return raw_input('Please enter your {}name\n'.format(name_type))

def user_name():
    result = get_name('fore'), get_name('middle'), get_name('sur')
    return ' '.join(result)

print('Result: ' + user_name())

The result would look like this:

Please enter your forename
Darren
Please enter your middlename
Coder
Please enter your surname
Macis
Result: Darren Coder Macis

Upvotes: 0

Rok Povsic
Rok Povsic

Reputation: 4895

Why do you have str parameters in every function? You don't use them, delete them so they don't clutter your code.

After doing that, you can call your methods only if you have () after their names, like this:

    result = ForeName(), MiddleName(), SurName()

You should set return value of a UserName(str) to something and then print that.

You probably want this:

result = UserName(str)
print "result %s" % (result)

(Or just UserName() if you delete the str parameter.)

Upvotes: 1

Related Questions