Hi in this article let's define a Python module, use the Python
module in your main program and call the functions present in the
module.
Let's try to solve this Python program.
write a python program to create a user defined module
I
am using a simple Python module to greet the user in different
methods, for example using hello/ welcome/ greetings etc defining a
function to each of the greetings methods.
This program can be made more Complex if needed but I will be writing it with simple for your understandability
Take
a file named hello.py and in the file write three functions to greet
the user fashion below. So whenever the module is important and
functions are called the user gets welcomed
Contents of hello.py
def say_hello(user):
print("Hello, ", user)
def display_welcome(user):
print("Welcome, ", user)
def display_greeting(user):
print(f"Greetings {user} !")
Take a new file named as main.py and import the above user defined module using the import keyword.
Take
a variable named as name and ask user to enter the username, now using
the function modules that we have defined in hello.py call all the
functions present in the user define module as shown below.
Contents of main.py
import hello
name = input("Please Enter Your Name : ")
hello.say_hello(name)
hello.display_welcome(name)
hello.display_greeting(name)
Output
===============
Please Enter Your Name : code with tj
Hello, code with tj
Welcome, code with tj
Greetings code with tj !
Output 2
===============
Please Enter Your Name : Akash
Hello, Akash
Welcome, Akash
Greetings Akash !
Conclusion
===========
The
above Python program defines a hello module that includes three
functions to greet the user. Just import the hello module and use all
the three functions to demo the user defined module.
Execute the above Python program by passing your name as input to the Python program and note down the results.
Comment it down below if you have any queries regarding the above Python program.
0 Comments