Little Turtle Python After the 17th lecture exercise

Source: Internet
Author: User

NOTES: 1, clear formal parameters and arguments 2, function documents: is part of the function, in the interpretation of different, using the Help (function name) or function name __doc__ can be viewed to 3, the keyword parameter (when a function of the parameters more obvious role): The name of the parameter is defined, for example: Def F ( Name,words) The following two kinds of reference methods are equivalent F (A, B) = f (words=b,name=a) 4, the default parameter: The function definition is the initial value of the parameter, if the function call without passing parameters, then automatically use the initial value Def F (Name=c,words=d) 5, Collect parameters: Def test (*params) test (1, ' Little Turtle ', 3.14,7,8,9) quiz: 0. Which of the following is the formal parameter and which is the argument?

def myfun (x):
return x * * 3
y = 3
Print (Myfun (y))

X is the formal parameter and y is the argument. Formal parameters refer to the arguments in the parentheses of the function creation and definition process, while the arguments refer to the parameters that the function passes in during the invocation.

1. What is the difference between a function document and a function write comment directly with "#"?

Writing a document to a function is a good habit for others to understand your function better:

def myfirstfunction (name):
' Function document at the very beginning of the function definition, denoted by an anonymous string '
Print (' I love fishc.com! ')

We see that the string Ta, written at the beginning of the function, is not printed, but TA is stored as part of the function, which we call the function document string, and the function of TA is the same as the annotation. The document string for a function can be accessed as follows:

>>> myfirstfunction.__doc__ #双低线
' Function document at the very beginning of the function definition, denoted by an anonymous string '

In addition, we use Help () to access this function to see the document string:

>>> Help (Myfirstfunction)
Help on function myfirstfunction in module __main__:
Myfirstfunction (name)
The function document is represented in the first part of the function definition with an anonymous string

2. Using keyword parameters, you can effectively avoid what problems arise?

Keyword parameter, refers to the function at the time of invocation, with the name of the parameter to specify which parameters to the specific call, so that the function can not be called according to the order of the parameters

3. Use Help (print) to view print () What are the default parameters for this bif? What is the role of each?

Help on built-in function print in module Builtins:

Print (...)
Print (value, ..., sep= ', end= ' \ n ', File=sys.stdout, Flush=false)

Prints the values to a stream, or to Sys.stdout by default.
Optional keyword arguments:
File:a File-like Object (stream); Defaults to the current sys.stdout.
Sep:string inserted between values, default a space.
End:string appended after the last value, default a newline.
Flush:whether to forcibly flush the stream.

4. What is the biggest difference between the default parameter and the keyword parameter surface?

The default parameter is the default value of the given formal parameter, which is the system error that causes the actual parameter to correspond to the formal argument and avoids the order errors.
The keyword argument is that when a function call is made, the parameter that needs to be assigned is set by the parameter name, which is not afraid of an error in the function call because the order of the arguments is unclear. The default parameter is to assign an initial value to the parameter in the process of defining the parameters, and when the function is called, the arguments are not passed, and the initial values of the formal parameters are used instead

Moving hands:

0. Write a function that meets the following requirements:

A) Calculates the result of printing all parameters and multiplying the cardinality (base=3)
b) If the last argument in the parameter is (base=5), the cardinality is set to 5, and the cardinality does not participate in the sum calculation.

No:

def Sum (*params,base=3):
result = 0
For I in params:
Result + = I
Return result*base

1. Find the number of daffodils

Title: If a 3-digit number is equal to the cubic sum of its members, it is called the number of daffodils. For example 153 = 1^3+5^3+3^3, so 153 is a narcissus number. Write a program to find out the number of daffodils.

Write for yourself:

def hua ():
For x in range (100,1000):
A =x%10
b =X%100//10
C =x//100
If x ==a**3+b**3+c**3:
Print (x)
Print (Hua ())

Little Turtle:
def daffodils ():
Print (' All daffodils number: ', end= ')
temp = 100
While temp < 1000:
if temp = = (temp//100) **3 + ((temp%100)//10) **3 + (temp%10) **3:
Print (temp,end= ")
Temp + = 1
Else
Temp + = 1
Print (daffodils ())

2. Write a function findstr () that counts the number of occurrences of a substring of length 2 in another string. For example: Assume that the input string is "you cannot improve your past, but can improve your the future." Once time is wasted, life is wasted. ", the substring is" im ", and the function executes after printing" The substring appears 3 times in the target string. "
Program execution Effect: No: Zoom out, compare by letter
def findstr ():
Print (' Please enter the target string: ', end= ')
temp = input ()
Print (' Please enter a substring (two characters): ', end= ')
comp = input ()
Count = 0
i = 0
For I in range (len (temp)):
If temp[i] = = Comp[0] and temp[i+1] = = Comp[1]:
Count + = 1
i + = 1
Else
i + = 1
count = Int (count)
Print (' substring total%d occurrences in target string '%count)
Findstr ()

Little Turtle:
def findStr (Desstr, SUBSTR):
Count = 0
length = Len (desstr)
If SubStr not in Desstr:
Print (' string not found in target string! ')
Else
For each1 in range (length):
If desstr[each1] = = Substr[0]:
If Desstr[each1 + 1] = = Substr[1]:
Count + = 1

Print (' substring has%d occurrences in target string '% count ')


DESSTR = input (' Please enter the target string: ')
SUBSTR = input (' Please enter a substring (two characters): ')
FindStr (Desstr, SUBSTR)

Little Turtle Python After the 17th lecture exercise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.