Hi Again, Dinko,
Well, here goes on the big lesson 4.0. I finished checking it just as the World
Cup match was about to start so, I will write this during the dull moments...
if there are any!
This time... I'll do suggestions in red italics and additions in plain red.
Caryl
-----------------------------------------------------------------------------So
far, we have written programs which write to the screen, but don't get any data
from the user. The communication was only one way: from the program to the
user. How do we write a program which gets data from the user and does
something with it? Here's one simple example. Let's just have the user type in
something and print it back to him.
Example 1
# example startname = raw_input("Please enter your name: ")print "Hello,",
name# example end
The raw_input function is what we need for that. When that function is
executed, the program pauses and waits for the user to write something. When
the user presses enter, the raw_input function returns to the program whatever
the user wrote. We stored the result of the raw_input function in the variable
"name". Also, notice the text we entered in the parentheses. What happens with
that text can better be seen in the following example:
Example 2
# example startprint "Please enter your name: "name = raw_input()print
"Hello,", name# example end
So, <name>, notice the two ways you can tell the user of your program that you
expect him to type in something. You can either make a print before raw_input
(example 1), or you can just make a raw_input and write your text in the
parentheses (example 2).
The text that was entered got stored in the variable. So far, we have only
stored numbers and True/False in variables. However, there are many different
things we can store in variables, not only numbers. I'll now show you a few
things you can do with text in Python. The reason why we're doing this now is
because raw_input stores anything you enter as text, even if you type in a
number (and a bit later we'll see what that means). Just one thing before we go
on: a sequence of characters is called a "string" in Python and many other
programming languages. In fact, it can even be a single character (so "asd" is
a string, and "a" is a string also) or an empty string (just "" - a string with
no characters).
Example 3
# example startvar = "Hello"var2 = "world"print var + var2# example end
Here we have two string variables. In Python, we can add strings together. That
gives us a new string which is created by simply putting the strings together,
the second one after the first. In Example 3, the two words are stuck to each
other when we print. How could we modify the strings to get a nice print?
Example 4
# example startvar1 = "Hello"var2 = " world"var3 = "!"var4 = var1 + var2 +
var3print var4# example end
Example 5
# example startprint "What happens when we multiply a string with a
number?"var1 = 10var2 = "spam"print var1 * var2# example end
OK, enough about strings for now, let's go back to inputting things from the
keyboard. You've already seen that we can add two numbers together and that we
can add two strings together. In Python, we can even multiply a string with a
number (remember, multiplying is just repeated adding). However, you can't add
or subtract a number and a string. What would that even mean? What is 2 +
"apple"? If you run the following code, you will get an error:
Example 6
# example startvar = "a string" + 5# example end
You should always carefully read the errors you get. They contain the reason
why the error happened, and you can easily find the part of code you should fix
if you understand the error. If you haven't already, run example 6 and read the
error you get. It will tell you that you cannot add an "int" (integer, or whole
number) and a "str" (string). **restart the tutorial or something after the
error? Or something else?*Perhaps you expected that the addition in Example 6
would result in the string "a string5". Well, we would get that if we had tried
to add "a string" (which is a string) and "5" (a one character string!),
because that's how we add two strings. However, written without the quotation
marks, 5 is a number, not a string. There's an easy way to turn numbers into
strings in Python.
Example 7
# example startvar = "a string" + str(5)print var# example end
Example 8
# example startvar = 10var2 = "a string" + str(var) + "another string"# example
end
There is a function called "str" which converts the value you give it into a
string, and we've used it here to convert 5, which is a number, into "5", which
is a string.There is also a way to do the opposite: convert a string into a
number. Look at the following example:
Example 9
# example startvar = "50"var2 = int(var)print var * 3print var2 * 3# example end
As you can see, "var" remained a string. The result of int(var) was a number,
and that number got stored in "var2".There's an easy way to check the types of
your variables:
Example 10:
# example startvar = "50"var2 = int(var)print "Type of var:", type(var)print
"Type of var2:", type(var2)# example end
There's an important reason why I'm telling you about converting numbers to
strings and strings to numbers. Remember what I told you about raw_input: "The
reason why we're doing this now is because raw_input stores anything you enter
as text, even if you type in a number (and a bit later we'll see what that
means)." It's a bit later now. :) As you've seen so far, we can't add numbers
and strings together, and raw_input always gives us strings. If we wanted to
get some numbers from keyboard and add them, we should write something like
this:
Example 11
# example startnumber1 = raw_input("Please enter the first number: ")number2 =
raw_input("Please enter the second number: ")print "These are two (three
actually) strings added: " + number1 + number2number1 = int(number1)number2 =
int(number2)print "These are two numbers added:", number1 + number2# example end
I suggest you treat this as two different concepts. At this point I would ask
them to think about why you said there were actually three strings.
The output from Example 12, below, is nothing like that from Example 11. At
this point, I would suggest you either modify the program so that that the
output will be of the form of an addition example, such as, "3 + 5 = 8", and
call it Example 12. Or, Challenge them to do it!
There are two more things I'd like to tell you before we finish this lesson.
The first one is just a tip to make your code neater: there's a shorter way to
do what we did in example 11. We can use int() directly on raw_input(), like
this:
Example 12 (change to 13 if you make a different 12)
# example startnumber = int(raw_input("Please enter a number: "))print "Let's
check the type:", type(number)# example end
The output of this is nothing like Example 11. Treat this as a different
concept.
It shouldn't matter (to Python) which approach you use. Use the one you like
more. You will develop your own programming style as you write more code, and
we'll also talk about styles which are generally accepted as good styles of
writing code.
You could Challenge them now to try to re-write the program in Example 11 using
the code they learned in Example 12.
The last thing I'd like to tell you in this lesson is how to check what the
user of your program entered. Go back to Example 12 (or 13), but don't enter a
number from your keyboard. Enter a letter or a word. Can you guess what will
happen?
If you did what I suggested you do, you've seen an error. Why? Because you
entered a string which could not be turned into a number. Normally, we want to
protect our programs from that. The user can accidentally enter something that
is not a number when he wanted to enter a number. In that case, we don't want
our program to crash, but to warn the user that he did not enter a number (you
would also probably want to ask him to repeat his input, we'll do that in the
next lesson). Let's see how that could be done. Try running the following
example and entering something that is not a number.
Example 13 (or 14?)
# example startprint "Please enter a number"num = raw_input()try: num =
int(num) print "All went well!"except: print "That can't be turned into a
number."# example end
(It would be nice to explain how to tab backwards rather than having to
backspace.)
This is usually called a try-except block. You may sometimes see people call it
a try-catch block, but it's "except" in Python. Well, what does it do? Python
_tries_ to execute the code inside the try block. If an error occurs, Python
doesn't crash, but executes the code which is under the except block. If no
errors occur, the except block never runs. This is very useful with parts of
code which may crash the entire program, and turning a string that the user
entered into a number is one such situation.
raw_input("Press enter to continue")print "Congratulations on finishing this
long lesson!"
_______________________________________________
IAEP -- It's An Education Project (not a laptop project!)
[email protected]
http://lists.sugarlabs.org/listinfo/iaep