Chapter 5: Making Your Program Interactive

Now that we’ve covered the basics of variables, let us write a program that makes use of them. We’ll revisit the “Hello World” program we wrote in Chapter 2, but this time we’ll make it interactive. Instead of just saying hello to the world, we want the world to know our names and ages too. In order to do that, our program needs to be able to prompt us for information and display them on the screen. Two built-in functions can do that for us: input() and print(). For now, let’s type the following program in IDLE. Save it and run it. myName = input(“Please enter your name: “) myAge = input(“What about your age: “) print (“Hello World, my name is” , myName, “and I am” , myAge, “years old.”) The program should prompt you for your name. Please enter your name: Supposed you entered James. Now press Enter and it’ll prompt you for your age. What about your age: Say you keyed in 20. Now press Enter again. You should get the following statement: Hello World, my name is James and I am 20 years old.

Input()

In the example above, we used the input() function twice to get our user’s name and age. myName = input(“Please enter your name: “) The string “Please enter your name: ” is the prompt that will be displayed on the screen to give instructions to the user. After the user enters the relevant information, this information is stored as a string in the variable myName. The next input statement prompts the user for his age and stores the information as a string in the variable myAge. The input() function differs slightly in Python 2 and Python 3. In Python 2, if you want to accept user input as a string, you have to use the raw_input() function instead.

Print()

The print() function is used to display information to users. It accepts zero or more expressions as parameters, separated by commas. In the statement below, we passed 5 parameters to the print() function. Can you identify them? print (“Hello World, my name is” , myName, “and I am” , myAge, “years old.”) The first is the string ”Hello World, my name is” The next is the variable myName declared using the input function earlier. Next is the string “and I am”, followed by the variable myAge and finally the string “years old.”. Note that we do not use quotation marks when referring to the variables myName and myAge. If you use quotation marks, you’ll get the output Hello World, my name is myName and I am myAge years old. instead, which is obviously not what we want. Another way to print a statement with variables is to use the % formatter we learned in Chapter 4. To achieve the same output as the first print statement above, we can write print (“Hello World, my name is %s and I am %s years old.” %(myName, myAge)) Finally, to print the same statement using the format() method, we write print (“Hello World, my name is {} and I am {} years old”.format(myName, myAge)) The print() function is another function that differs in Python 2 and Python 3. In Python 2, you’ll write it without brackets, like this: print “Hello World, my name is ” + myName + ” and I am ” + myAge + ” years old.”

Triples Quotes

If you need to display a long message, you can use the triple-quote symbol (‘’’ or “””) to span your message over multiple lines. For instance, print (‘’’Hello World. My name is James and I am 20 years old.’’’) will give you Hello World. My name is James and I am 20 years old. This helps to increase the readability of your message.

Escape Characters

Sometimes we may need to print some special “unprintable” characters such as a tab or a newline. In this case, you need to use the \ (backslash) character to escape characters that otherwise have a different meaning. For instance to print a tab, we type the backslash character before the letter t, like this: \t. Without the \ character, the letter t will be printed. With it, a tab is printed. Hence, if you type print (‘Hello\tWorld’), you’ll get Hello World Other common uses of the backslash character are shown below. >>> shows the command and the following lines show the output. \n (Prints a newline) >>> print (‘Hello\nWorld’) Hello World \\ (Prints the backslash character itself) >>> print (‘\\’) \ \” (Prints double quote, so that the double quote does not signal the end of the string) >>> print (“I am 5’9\” tall”) I am 5’9” tall \’ (Print single quote, so that the single quote does not signal the end of the string) >>> print (‘I am 5\’9” tall’) I am 5’9” tall If you do not want characters preceded by the \ character to be interpreted as special characters, you can use raw strings by adding an r before the first quote. For instance, if you do not want \t to be interpreted as a tab, you should type print (r‘Hello\tWorld’). You will get Hello\tWorld as the output.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top