Saturday, September 6, 2014

Python Tutorial 2 – Getting user Inputs



                       Hey, welcome back to Mohammed’s Python tutorial series. I hope you enjoyed the first tutorial. In this tutorial we will do a simple program which let users input/type in/ their name and greets them using their name. If you have your IDE fired up then let’s get started.
           Here is the source code for the simple program
(Source code)
print("Please enter your name.")
name = input()
print("Hello "+name)

       And when you run this >>
 
                     If you typed in the code correctly then this should appear. And by the way you can input in anything and it will greet it. So now let’s go through the code.
         The first line is a piece of code which prints Please enter your name”. On the second line we store whatever the user inputs in the variablename’ .You can name your variable any name. And then we ask the user to write his name using the input() function .
       On the last line we have a print() function again. This time, we use the plus operator (+) to concatenate the string 'Hello ' and the string stored in the  namevariable, which is the name that our user input into the program.

           All in all, what we just did was create a variable called name, which is  used to store whatever name the user inputs, and then ask user to input his/her name, store the name in ‘name’ string and then print Hello + the name the user just typed. Very simple I know.
            If you have questions comment them, Goodbye.

No comments:

Post a Comment