Python is a simple programming tool available on Raspberry Pi. We take you through an easy example that takes input, displays it, and does a check on one of the inputs given during the running of the program.
Login to the RPi (either locally or via SSH – it does not matter for the tutorial) and then enter:
pi@raspberrypi ~ $ nano example.py
This will open a text editor called “nano” and create a new python file called “example.py”. All python scripts end with .py as this tells the system how to handle the contents.
We are going to enter a simple script, here it is completed:
#!/usr/bin/python import sys def prompt(): response = sys.stdin.readline().strip() return response fields = [ "Please tell me your name: ", "what school do you go to?: ", "What year are you in?: " ] answers =  for field in fields: print field, v = prompt() answers += [v] print """Hello %s! You go to %s and you are in year %s""" % ( answers, answers, answers ) year = int(answers) if year == 9: print "Hope your GCSE\'s are going well." elif year == 8: print "I hope you are studing for your exams!" else: print "Thanks for playing"
Lets go through what each line means and is doing
#!/usr/bin/python import sys
Line 1 : this tells the RPi to run this file as a python script and where python is installed (this is standard and rarely changes) so you can just type this line in.
Line 2 : this imports a python package called sys – this contains some common scripts that means we do not have to write them manually
Now we get to the main part of the script, with Lines 4-6 we are setting up a re-usable function called “prompt” that we will call later in the script. Functions are useful as you can call them over and over again without re-writing the code. So knowing functions can be vary useful. Lets go into each line:
def prompt(): response = sys.stdin.readline().strip() return response
Line 4: This is setting up the system to use a standard function called “prompt” this is a function that will ask for some input when the script is run.
Line 5: With this line we taking the response of the user running the script and stripping out any strange responses (Like carriage returns etc.)
Line 6: We are returning the result of the function
fields = [ "Please tell me your name: ", "what school do you go to?: ", "What year are you in?: " ]
Line 8: Here we are storing a list of strings that the script will use later – they could ask anything you wish, but in our code we are asking the users name, school and what school year they are in.
answers =  for field in fields: print field, v = prompt() answers += [v]
Line 10: Here we are preparing the script to set a list (or array) called “Answers”
Line 11: This line will start a loop that will loop around for each question that we stored in the list called “fields” back on line 8. We are saying for each single question in the array of questions do the following
In python when you are doing things in a loop as here, you indent your code with 4 spaces (not a tab!). Python will run anything indented in the loop
Line 12: Print to the screen the question (first loop will ask your name, second loop your school and third loop your school year)
Line 13: Wait for the user to type in their answer and temporally store it in a string called “v”
Line 14: Put the temporary answer V into the next slot of the string list (array). First loop will store it as answers, second loop answers and so on until all answers have been stored.
Ok lets recap what we have done so far. We have created a new script file in nano and set up the script to ask the user some questions. These questions are stored in an array (or string list) so that we can respond with the answers later. The questions and thus what responses you get are easy to change. But currently we are storing all answers as strings. Later on we will be converting one of the strings into a number (or integer).
Lets carry on to the end of the script
print """Hello %s! You go to %s and you are in year %s""" % ( answers, answers, answers )
Line 16: We start a multi line command that will print some text to the screen. Since it is multi line we need to use three quote marks (“). The %s will be replaced by one of the answers we placed into the string list
Line 17: We print a second line to the screen – again the %s will be replaced.
Line 18: We print another line to the screen, the three ending quotes (“) tell the script we have finished wring the text to the screen. the % is telling python that we are now specifying what to replace the three %s’s with. You can see we are calling the string list answers. The number in the square brackets , tells it what string the print. It will read it in order so answers will print the users name after Hello.
So now you can get some input from the user and print it to the screen. But you can do more with the response. You can use it to decide on what to print as I will show below
year = int(answers)
Line 20: Here we are taking the response of the school year and turning it into an integer so we can use it later. We are calling this integer “year”.
if year == 9: print "Hope your GCSE\'s are going well." elif year == 8: print "I hope you are studying for your exams!" else: print "Thanks for playing"
Line 22: We are stating a set of checks on the integer stored in year, we are asking the script to check if “9” was entered. if it was it will go onto line 23. If not it will move to line 24.
Line 23: Since the user said they’re in year 9, print a statement to the screen “hope your GCSE’s are going well.” You will notice that GCSE’s has a backwards slash “GCSE\’s) before the apostrophe. This is called an escape code and it is required as we are using a character that could be code or text. We are telling the script it is text.
Line 24 and Line 25: Since the user said they’re in 8 we print a different statement to the screen. elif is code for Else If. So since the script did not find the year as 9, check to see if they said 8.
Line 26: If they entered something else in the year just say thank you for playing.
Now you have typed in the code press the following
ctrl+0 (that O not zero)
That will save the contents of the screen into the file. Now press
That will close the nano text editor
Now your ready to run your code, so enter:
pi@raspberrypi ~ $ python example.py
and the script will run!