Home > Blockchain >  How to combine 2 strings from different functions in Python
How to combine 2 strings from different functions in Python

Time:09-16

I'm trying to combine two user input strings from different functions and then have it print as one statement. I know to combine two regular strings (ones not in a function) I would use the ' ' function, but I can't figure out how to do it with user input being from two different functions. Can anyone help? here is my code:

def get_first_name(first_name):
    first_name = str(input('what is your first name?'))
    len_firstName = 0
    while len_firstName <= 0:
            print('that is not valid, please try again.')
            first_name = str(input('what is your first name?'))
            len_firstName = len(first_name)
            print(len(first_name))
    return first_name

get_first_name(input)


def get_last_name(last_name):
    last_name = str(input('what is your last name?'))
    count_of_last_name = len(last_name)
    if count_of_last_name < 1: 
        print('There is no last name.')
    else:
        print(last_name)
    return last_name

get_last_name(input)

# This is what I am trying to do to combine the two but it doesn't work and I'm stuck:

full_name = get_first_name   get_last_name
print(full_name)

CodePudding user response:

The main misunderstanding here seems mostly based around how functions should be structured. To get the behavior you want, define two distinct functions: get_first_name and get_last_name. These functions should accept no parameters, and return a string which you read from input.

You can then define a get_full_name function where you call each, and combine their results. Call this function, remembering that function calls must end with parentheses. Your result may look something like this! Notice how the print statement is not inside a function definition, so it is executed when you run your program.

# define functions
def get_first_name():
  first_name = input("What is your first name? ")
  return first_name

def get_last_name():
  last_name = input("What is your last name? ")
  return last_name

def get_full_name():
  full_name = get_first_name()   " "   get_last_name()
  return full_name

# call your full_name function
print("Hello, "   get_full_name())

Let me know if any of that is unclear!

CodePudding user response:

You were almost there. I change your string line and it worked.

def get_first_name(first_name):
    first_name = str(input('what is your first name?'))
    len_firstName = 0
    while len_firstName > 0:
            print('that is not valid, please try again.')
            first_name = str(input('what is your first name?'))
            len_firstName = len(first_name)
            print(len(first_name))
    return first_name

def get_last_name(last_name):
    last_name = str(input('what is your last name?'))
    count_of_last_name = len(last_name)
    if count_of_last_name < 1: 
        print('There is no last name.')
    else:
        print(last_name)
    return last_name

# This is what I am trying to do to combine the two but it doesn't work and I'm stuck:

full_name = get_first_name(input)   " "   get_last_name(input)   

#Basically, you never stored/assigned the return values of your functions to someting

print(full_name)

CodePudding user response:

I would say Ethan answerd your question about why the functions dont work perfectly. What I'd like to add is a reworked version of your first function

def get_first_name():
    first_name = ''
    while len(first_name) == 0:
        first_name = input('what is your first name?\n')
        if len(first_name) == 0:
            print('that is not valid, please try again.')

In your version you'd always get a 'not valid' message even if the input is valid.

As to why your functions don't need any attributes, maybe my silly painting might help :)

or this code:

def output(first_name):
    print('my first name is '   first_name)

my_name = 'ovski'

output(my_name)

The (first_name) in def output is an attribute that is used inside the function.

In other words:

  • 'ovski' is stored inside the variable my_name
  • my_name is given as attribute into the function output
  • output uses the values inside the attribute and - combines it with the string 'my first name is '

function with attribute

  • Related