<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
</head>
<body class='hmmessage'>
<div>Hi Again, Dinko,</div><div><br></div><div>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!</div><div><br></div><div>This time... I'll do suggestions in <i><font class="Apple-style-span" color="#C00000">red italics</font></i> and additions in <font class="Apple-style-span" color="#C00000">plain red.</font></div><div><font class="Apple-style-span" color="#C00000"><br></font></div><div>Caryl</div><div><br></div><div>-----------------------------------------------------------------------------</div><div>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.</div><div><br></div><div>Example 1</div><div><br></div><div># example start</div><div>name = raw_input("Please enter your name: ")</div><div>print "Hello,", name</div><div># example end</div><div><br></div><div>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:</div><div><br></div><div>Example 2</div><div><br></div><div># example start</div><div>print "Please enter your name: "</div><div>name = raw_input()</div><div>print "Hello,", name</div><div># example end</div><div><br></div><div>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).</div><div><br></div><div>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 <font class="Apple-style-span" color="#C00000">and many other programming languages.</font> 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).</div><div><br></div><div>Example 3</div><div><br></div><div># example start</div><div>var = "Hello"</div><div>var2 = "world"</div><div>print var + var2</div><div># example end</div><div><br></div><div>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?</div><div><br></div><div>Example 4</div><div><br></div><div># example start</div><div>var1 = "Hello"</div><div>var2 = " world"</div><div>var3 = "!"</div><div>var4 = var1 + var2 + var3</div><div>print var4</div><div># example end</div><div><br></div><div>Example 5</div><div><br></div><div># example start</div><div>print "What happens when we multiply a string with a number?"</div><div>var1 = 10</div><div>var2 = "spam"</div><div>print var1 * var2</div><div># example end</div><div><br></div><div>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:</div><div><br></div><div>Example 6</div><div><br></div><div># example start</div><div>var = "a string" + 5</div><div># example end</div><div><br></div><div>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?*</div><div>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.</div><div><br></div><div>Example 7</div><div><br></div><div># example start</div><div>var = "a string" + str(5)</div><div>print var</div><div># example end</div><div><br></div><div>Example 8</div><div><br></div><div># example start</div><div>var = 10</div><div>var2 = "a string" + str(var) + "another string"</div><div># example end</div><div><br></div><div>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.</div><div>There is also a way to do the opposite: convert a string into a number. Look at the following example:</div><div><br></div><div>Example 9</div><div><br></div><div># example start</div><div>var = "50"</div><div>var2 = int(var)</div><div>print var * 3</div><div>print var2 * 3</div><div># example end</div><div><br></div><div>As you can see, "var" remained a string. The result of int(var) was a number, and that number got stored in "var2".</div><div>There's an easy way to check the types of your variables:</div><div><br></div><div>Example 10:</div><div><br></div><div># example start</div><div>var = "50"</div><div>var2 = int(var)</div><div>print "Type of var:", type(var)</div><div>print "Type of var2:", type(var2)</div><div># example end</div><div><br></div><div>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:</div><div><br></div><div>Example 11</div><div><br></div><div># example start</div><div>number1 = raw_input("Please enter the first number: ")</div><div>number2 = raw_input("Please enter the second number: ")</div><div>print "These are two (three actually) strings added: " + number1 + number2</div><div>number1 = int(number1)</div><div>number2 = int(number2)</div><div>print "These are two numbers added:", number1 + number2</div><div># example end</div><div><br></div><div><font class="Apple-style-span" color="#C00000"><i>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. </i></font></div><div><font class="Apple-style-span" color="#C00000"><i><br></i></font></div><div><font class="Apple-style-span" color="#C00000"><i>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 </i></font><span class="Apple-style-span" style="color:rgb(192, 0, 0)"><i>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!</i></span></div><div><br></div><div>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:</div><div><br></div><div>Example 12 <font class="Apple-style-span" color="#C00000"><i>(change to 13 if you make a different 12)</i></font></div><div><br></div><div># example start</div><div>number = int(raw_input("Please enter a number: "))</div><div>print "Let's check the type:", type(number)</div><div># example end</div><div><br></div><div><font class="Apple-style-span" color="#C00000"><i>The output of this is nothing like Example 11. Treat this as a different concept.</i> </font></div><div><br></div><div>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.</div><div><br></div><div><font class="Apple-style-span" color="#C00000"><i>You could Challenge them now to try to re-write the program in Example 11 using the code they learned in Example 12.</i></font></div><div><br></div><div>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 <font class="Apple-style-span" color="#C00000"><i>(or 13)</i></font>, but don't enter a number from your keyboard. Enter a letter or a word. Can you guess what will happen?</div><div><br></div><div>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.</div><div><br></div><div>Example 13 <font class="Apple-style-span" color="#C00000"><i>(or 14?)</i></font></div><div><br></div><div># example start</div><div>print "Please enter a number"</div><div>num = raw_input()</div><div>try:</div><div> num = int(num)</div><div> print "All went well!"</div><div>except:</div><div> print "That can't be turned into a number."</div><div># example end</div><div><br></div><div><font class="Apple-style-span" color="#C00000"><i>(It would be nice to explain how to tab backwards rather than having to backspace.)</i></font></div><div><br></div><div>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.</div><div><br></div><div>raw_input("Press enter to continue")</div><div>print "Congratulations on finishing this long lesson!"</div><div><br></div>                                            </body>
</html>