Lesson 13 Introduction to Programming Student Resources



Download 0.83 Mb.
Page4/9
Date28.05.2018
Size0.83 Mb.
#51381
1   2   3   4   5   6   7   8   9

Input from the User

Now let’s try adding an input from you, the user, to a variable. The format may look strange, but the word input is actually a call to a function. By using the word input, we are invoking an already written piece of code whose job is to obtain input and return it. What is placed in the parentheses is a prompting message that will appear in the console window before you type in the input. This prompting message is useful for instructing the user on what to do. Type the following sequence:



  • name2=input("Enter your name: ")
    We take the value returned and store it in the variable name2. Make sure you type the instruction exactly as shown, including two blank spaces after the colon (:) following the word name.

  • After entering the above command, you will see 'Enter your name: ' in the console window. At this point, type a friend’s name and press the Enter key. You will not see any output.

  • Another function is called print. Although we can output the value of a variable just by typing in the variable name, the print function is more useful in that we can use it to output a number of things. Type the following. Notice that there is a blank space after the word is. There are no blank spaces following the word print, the second quotation mark symbol, or the plus sign.

print("My friend is "+name2)

What do you see?





If-Statements and If/Else-Statements

Another feature of a program is to decide what should be done based on values stored in variables. For instance, we might want our program to decide and tell us if, based on our age, we should go to recess or to the library. To make a decision, programs use if-statements and if/else-statements. In Python, the syntax is a little bit odd and requires using proper indentation. Type the following after the prompt (>>>), and make sure you use the Tab key to indent when needed. The prompt and the ellipses (…) are automatically displayed; they are not something for you to type.



  • >>> if age >= 15:

  • ... print("Go to the library and study")

  • ... else:

  • ... print("Go play at recess")

  • ...

The result should be either the message about going to the library or the message about recess, depending on the value stored in age. Since age should now be 16, the message should tell you to go to the library and study. Notice that the notation >= means “greater than or equal to.”

Now try this. Type:



  • age=14
    Now retype the if-then-else statement above.

What output did you get this time? Notice how the computer makes a choice of what to do. The if-then-else statement is a very powerful tool that we use throughout a program. These statements allow the program to make decisions on its own.

For-Loops

Another type of feature of a program is known as a loop. A loop causes a set of code to operate a certain number of times. Several types of loops exist, but we will look only at one type of loop in Python called an iterator loop. This loop, known as a for-loop, will iterate once for each item in a list. Let’s try it out with the following code. Make sure to press the Tab key for the second line, and remember that the ellipses (…) are automatically placed there, so do not type them yourself.



  • >>> for number in [0, 1, 2, 3, 4]:

  • ... print(number)

  • ...

This will print 0, 1, 2, 3, and 4 on separate lines.

The for-loop can operate on lists that contain numbers, strings, and individual characters. Type the following two for-loops, adding a tab for each second line:



  • >>> for item in [1, 10, 'a', 'abc']:

  • ... print(item)

  • ...

  • >>> for item in name:

  • ... print(item)

  • ...

What do you see? (Remember that for our example, the variable name had Emily Smith stored in it. Your variable name should have your own name.)

Another function is called range( ). This function returns a list of all of the numbers in the range from 0 to one less than the number you place in parentheses ( ). Type the following:



  • for number in range(5):

  • ... print(number)



And you will see, 0, 1, 2, 3, 4 print out. You can also specify the starting point for a range. Retype the previous code but use range(1,6) and you will get the numbers 1 through 5 as output. Let’s use this idea to perform a computation called a factorial. A factorial is the value we get by multiplying a number by all of the numbers less than it down to 1. For instance, the factorial for 5 is 5*4*3*2*1. We write this value as 5!. The following code computes and outputs 5!. Remember to include the tab in the third line.

  • factorial=1

  • for value in [1, 2, 3, 4, 5]:

  • ... factorial=factorial * value



  • print(factorial)

The result should be 120.


Download 0.83 Mb.

Share with your friends:
1   2   3   4   5   6   7   8   9




The database is protected by copyright ©ininet.org 2024
send message

    Main page