Sunday, July 24, 2016

Taking Input



Before we take an input from a user, a little explanation regarding the Python versions installed on a Raspberry PI.

There are two of them: one is Python 2.7.x and the other is Python 3.4.x. (as of writing this post).

There are differences between them, so from now on let's use the higher version as it is supposed to be the version that replaces the older Python version 2.

What's different about it? We did not learn much so we should not see much of a difference. In previous posts, you noticed that we printed stuff using parentheses () when using Python3 and we did not use them using Python (which is version 2).

Open a text editor (you can use Leafpad text editor from a tool bar).


In the text editor let's type in the following:
print('Hello, and welcome. What is your name?')
name = input()
print('It is nice to meet you', name)
print('I hope you are digging Python programming!')

Save it as hello.py file in your home directory. Then execut this from the terminal using the following command:

pi@tron ~ $ python3 hello.py

Watch what happens!
 
pi@tron ~ $ python3 hello.py
Hello, and welcome. What is your name?

And now the computer awaits for your answer. Type in a name (for instance Jim) and watch what happens next.

The command input():
name = input() 
awaits for the user to type some text and then it is going to store it in the variable (name). Only then it proceeds to the next command we typed in our hello.py file

print('It is nice to meet you', name) 

Then, next step the computer does is to display text enclosed in quotes followed by , (comma separator) followed by the variable name.

print('It is nice to meet you', name)

Those display the text stored in variable name

Problem Solving
Write a code that displays similar greeting. Omit pieces of code such as closing parentheses or quote mark. See what errors they generate.