官术网_书友最值得收藏!

Build your own function – name()

So, you have learned about variables and how they store information. You have also learned about how these variables can be used inside of a function. Finally, you have learned how to use special Python functions, such as input(), to help get information from users and store it in the computer. You are ready to build your own function using variables and input().

Set up your project file

The function that we will build now is called name(). The purpose of this function will be to ask the user their name, store (remember) the name, and then print out a friendly message to the user.

To start this function, do the following:

  1. Open a new file in your text editor.
  2. Go to Save and name the file name.py.
    Tip

    You need to use .py at the end of all of your code files so that the files run in the terminal/command prompt. Python only recognizes .py files.

  3. Save the file in the folder you made for all of your Python work.

Begin your project

Once you have set up a project file, the first thing you might want to do is add a short comment to your file. A comment allows humans to quickly understand what is happening in the code. Whenever you are writing something that is not code, you should start the line with a hashtag, or hash. The hash is one way to tell the computer, Ignore this!, yet it allows humans to read the text. Type the following line in your file:

# This is my first function called name. It will ask the name and # print a message.

Writing code

The next line you type will begin the computer-readable code. First, make sure that there is a space between the comment you wrote and the first line of computer-readable code. As we learned earlier, you will start the function using the Python word def. Then, you will type one space and the name of the function:

 def name

Next, you will add parentheses () and a colon : to the first line:

 def name():

Now, it is time to go to the next line. For the next line, you will need to indent. Use the spacebar to insert four spaces. In Python, spaces matter. Using the Tab key, or mixing between tab and space, is a problem in Python and causes errors.

Since we are asking the user for their first name, you can use the words first_name for the variable if you like:

 def name():
 first_name =

The first_name variable will store the answer to the question, What is your first name? Remember, though, we have to use the raw_input() function to get the user to answer the question! So, we will add the raw_input() function and question to the code:

 def name():
 first_name = input('What is your first name?')

So far, we have programmed a way for the computer to ask the user for their first name, and we have made a variable called first_name to remember the string of information.

Even though we have a file that has some lines of code, if we were to run our code right now, nothing at all would happen. We need a way to show the user their name, and it would be even nicer if we sent the user a welcoming message. We need to write the code for program output.

We have been using print to output our information from Chapter 1, Welcome! Let's Get Started, and throughout this chapter, and print is also useful here. We can tell our function to print the first_name information, and we can put that together with a nice message. Add this line to your code:

 print('So nice to meet you, ' + first_name)

Your total code for the name() function should look like this:

def name():
 first_name = input('What is your first name?')
 print('So nice to meet you, ' + first_name)

Here is a sample of how the program looks in a text editor:

We need only to add the final line of code, which is to call the name() function. If we do not call the function, it will not run. To call the function, you should leave an empty line after print, and on a new line, unindent and type name(). Take a look at this code sample, and add the name() function to your code:

def name():
 first_name = input('What is your first name?')
 print('So nice to meet you, ' + first_name)

name()

Now that we have created this function, we can use it to greet anybody because the user is telling us each time what first_name should be. We have made a reusable block of code.

Running your program

Now you have to save your work:

  1. Go to the Save option in your text editor and save the work for name.py.
  2. Once you have saved this work, you should go to your terminal/command prompt.
  3. Make sure that you are in the correct folder.
    Tip

    If you are not sure, you can type pwd (Mac/Linux) or echo %cd% (Windows) to find out what folder you are in.

  4. When you are in the same folder as your work, type:
     python name.py
    
  5. Then, press Enter. Your program should begin to run.

Once you type in the name, the output should look like this:

You now have a program to share with family and friends that will seem amazing, especially if they have never programmed before!

主站蜘蛛池模板: 咸阳市| 麻栗坡县| 房产| 海兴县| 贡山| 石家庄市| 齐齐哈尔市| 信阳市| 定南县| 灌南县| 额济纳旗| 高淳县| 乌兰县| 临澧县| 石泉县| 彭山县| 太谷县| 额敏县| 织金县| 繁昌县| 南部县| 正宁县| 内江市| 乌兰浩特市| 上虞市| 阿合奇县| 体育| 沈阳市| 科技| 黔西县| 修文县| 丹巴县| 美姑县| 绍兴县| 拉萨市| 精河县| 兰考县| 庄河市| 石城县| 资兴市| 青岛市|