A Python program to ask the user to enter name
Here I am going to explain a Python program to ask the user to enter a name. The task is going to be a very easy one. I will explain to you about the commands used in this program and how to write this program using a function.
A simple program to ask for the name in Python
NAME=str(input("enter the name: ")) print("hello",NAME)
enter the name: user hello user
The general description of this program is to ask a user name and save the data in “NAME” and to print the name using the variable where the data is stored.”NAME” is just a variable where the data is stored. Since we are asking name where no numbers are allowed so I am using string data type.
Ask the user to enter name in Python using a function
Let’s create our simple custom Python function and then call it:
def hello(): name=str(input("enter the name : ")) print("hello " + str(name)) return hello()
Run this code online
And the output will be like you can see below:
enter the name : user hello user
Here I had altered the general program by using a function. The hello() is a user-defined function used in this program and the function generated in a custom way. In this function, it is programmed to get the user name and to print the user name with a string “hello”.
So you can see that the same functionality is done by defining our own function.
I hope that I had demonstrated the program using function and in a simple way for your understanding.