How to use a function inside another function in python3?

0

I wanted to get the result of the input of the function name and use inside the intro function, however the way I'm trying, I get only one error.

code:

def nome():
    name = str(input('Poderia nos dizer seu nome? '))
    return name

def intro():
    nome()
    print('Olá {}, seja bem vindo!'.format(name))


intro()

And this is the error I got as a result:

I'm a beginner in the area, thank you in advance.

    
asked by anonymous 23.11.2018 / 19:16

2 answers

8

If in the nome() function you returns the read value, you need to store this value in a variable. The name variable you set within nome() does not exist within intro() . They are different scopes.

def nome():
    name = input('Poderia nos dizer seu nome? ')
    return name

def intro():
    name = nome()
    print('Olá {}, seja bem vindo!'.format(name))


intro()

Notice that I removed str() from input too, as this is redundant; the return of input will always be a string .

    
23.11.2018 / 19:18
0

For your code to work, do so:

def nome():
    name = input('Poderia nos dizer seu nome? ')
    return name

def intro(name):
    print('Olá {}, seja bem vindo!'.format(name))

name = nome()
intro(name)

Note that the variable name is being passed as a parameter for the input fuction ()

Also, in python you are allowed to create nested functions and your code could look like this:

def intro():
    name = nome()
    print('Olá {}, seja bem vindo!'.format(name))

    def nome():
        name = input('Poderia nos dizer seu nome? ')
        return name

# aqui a chamada da função intro
intro()

And be happy ...

    
24.11.2018 / 19:59