What is a program code and how it works? (part 2)
I like to compare running programs to a network of roads. User can use the program == drive in the roads and take turns in crossings but he can’t drive just anywhere and in any way possible. So, coding is like building those roads and adding roundabouts, crossings, traffic lights and tunnels where only certain high vehicles can go. And maybe adding some road tolls.
Here is a silly example of a program done with no real programming language (no computer will understand this in this kind of a “code” ) and also with Python-language:
Instructions for a computer:
|1. Ask a number from user.||
print "Type a number, please."
|2. Save the mark that user types.||
typed_mark_1 = raw_input( )
|3. Make sure it was a number and not a letter or something,||
while not typed_mark_1.isdigit():
|4. If it wasn’t a number, ask politely a new number and do this until user gives a real number.||
print "That wasn't a number, please type a number." typed_mark_1 = raw_input( )
|5. Then ask another number.||
print "Type an other number, please." typed_mark_2 = raw_input( )
|6. Make again sure it is a number, and ask a new one if it isn’t.||
while not typed_mark_2.isdigit(): print "That wasn't a number, please type a number." typed_mark_2 = raw_input( )
|7. Add the two numbers, you have got, together.||
number_1 = int( typed_mark_1 ) number_2 = int( typed_mark_2 ) addition = number_1 + number_2
|8. Print the result to the screen.||
There you have it, step by step. Of course computer can’t actually understand the pseudocode on the left side, but it could understand the Python code on the right side (I'll tell you more about how to make computers understand certain languages later on).
Later I'll tell more about the order of the text / code, also using this example. So, don't worry if the Python-version is still unclear to your. :)
07.03.2012 Edited text to match the code.
07.03.2012 Added real python code.