Chapter 4 Functions, 第四章 函數。

Chapter 4 Functions, 第四章 函數。

 In this chapter you are going to:
  • learn about functions 
  • write your own functions 
  • create a number guessing game. 

Functions

You have already met and used a few functions. The first one you used was print(). Functions have brackets after their name. This is where we supply arguments separated by commas. Some functions do not need them, they do their jobs without argument!
There are many functions that are built in to Python that we can already use. We can also make our own. We create functions with the def keyword. Here is the code for a counting function.


印= print
number= 10
print("This is my number: ", number)
數= 10
印("這是我的數: ", 數)
def count_to(a_number):
    n=1
    while n <= a_number:
        print(n)
        n = n+1
def 算到(一個數):
    n= 1
    while n <= 一個數:
        印(n)
        n= n+1
count_to(10)
算到(10)

In interactive mode, type in the above code. You will need to press return twice to get back to the Python prompt (>>>). Then type count(10) and press return.

沒有留言:

張貼留言