Python programming Quick Start-making tedious work automation chapter three function exercises and their answers

Source: Internet
Author: User

Chapter three function exercises and their answers 1. Why is it good to add a function to a program?

A: The function reduces the duplicated code. This makes the program shorter, easier to read, and easier to modify.

2. When does the code in the function execute: When the function is defined, or when the function is invoked?

A: The code in the function executes when the function is invoked, not when the function is defined.

3. What statement creates a function?

A: The DEF statement defines (creates) a function.

4, what is the difference between a function and a function call?

A: The function contains the DEF statement and the code in the DEF clause. The function call lets the program execute go inside the function, and the function call evaluates to the function's return value.

5. How many global scopes are there in the Python program? How many local scopes are there?

A: When invoking a function, a global function and a local scope are created.

6. What happens to a variable in the local scope when the function call returns?

Answer: When the function returns, the local scope is destroyed, and all the variables are forgotten.

7. What is the return value? Can the return value be part of an expression?

Answer: The result of the function call evaluation when the value is returned. Like all values, the return value can be part of an expression.

8. If the function does not return a statement, what is the return value of the call to it?

A: If the function does not have a return statement, its return value is none.

9. How to force a variable in a function to refer to a global variable?

A: The global statement forces a variable in the function to refer to the Globals variable.

10. What is the data type of none?

A: The data type of none is none.

11. What does the import areallyourpetsnamederic statement do?

A: Import statement imports the Areallyourpetsnamederic module (by the way, this is not a real Python module).

12, if in a module named spam, there is a function named Bacon (), after the introduction of span, how to call it?

A: The function can be called through Spam.bacon ().

13. How to prevent the program from crashing when encountering an error?

A: Place the line of code that could cause the error in a try clause.

14. What happens in the try clause? What happens in the except clause?

A: The code that could cause the error is placed in a try clause. When an error occurs, the code to be executed is placed in the EXCEPT clause.

3.11 Practice projects as a practice, please write a program to complete the following tasks 3.11.1 Collatz sequence

Write a function called Collatz (), which has a parameter named number. If the parameter is even, then Collatz () prints the NUMBER//2,
and returns the value. If number is odd, Collatz () prints and returns 3*number + 1
Then write a program that lets the user enter an integer and constantly call Collatz () on that number until the function returns a value of 1 (surprisingly, this sequence
For any integer to be valid, take advantage of this sequence, you will get 1 sooner or later! Even mathematicians are not sure why. Your program is studying the so-called "Collatz sequence",
It is sometimes referred to as "The simplest, impossible mathematical problem").
Remember to convert the return value of input () to an integer using the INT function, or it will be a string.
Tip: If number% 2 = = 0, integer number is even. If number% 2 = = 1, it is odd.
For:

#!/usr/bin/env Python3#-*-coding:utf-8-*-#Author:davie"""write a function called Collatz (), which has a parameter named number. If the parameter is an even number, then Collatz () prints NUMBER//2 and returns the value. If number is odd, Collatz () prints and returns 3*number + 1 and then writes a program that lets the user enter an integer and constantly calls Collatz () on that number until the function returns a value of 1 (surprisingly, this sequence is valid for any integer, With this sequence, you'll get 1 sooner or later! Even mathematicians are not sure why.    Your program is studying the so-called "Collatz sequence", which is sometimes referred to as "The simplest, impossible mathematical problem". Remember to convert the return value of input () to an integer using the INT function, or it will be a string. Tip: If number% 2 = = 0, integer number is even. If number% 2 = = 1, it is odd. """defCollatz (number):ifNumber = = 1:        return1elifNumber% 2 = =0:returnNumber//2elifNumber% 2 = = 1:        return3*number + 1Print(Collatz (18))Print(Collatz (17))
3.11.2 Input Validation

Add a try and except statement in the previous project to detect if the user has entered a string that is not an integer. Normally, an int () function generates a valueerror error when passing in a non-integer string, such as an int (' Puppy '). In the EXECPT clause, output a message to the user telling them that an integer must be entered.

For:

#!/usr/bin/env Python3#-*-coding:utf-8-*-#Author:davie"""write a function called Collatz (), which has a parameter named number. If the parameter is an even number, then Collatz () prints NUMBER//2 and returns the value. If number is odd, Collatz () prints and returns 3*number + 1 and then writes a program that lets the user enter an integer and constantly calls Collatz () on that number until the function returns a value of 1 (surprisingly, this sequence is valid for any integer, With this sequence, you'll get 1 sooner or later! Even mathematicians are not sure why.    Your program is studying the so-called "Collatz sequence", which is sometimes referred to as "The simplest, impossible mathematical problem". Remember to convert the return value of input () to an integer using the INT function, or it will be a string. Tip: If number% 2 = = 0, integer number is even. If number% 2 = = 1, it is odd. """defCollatz (number):ifNumber = = 1:        return1elifNumber% 2 = =0:numbers= number//2Print(Numbers) Collatz (numbers)elifNumber% 2 = = 1: Numbers= 3*number + 1Print(Numbers) Collatz (numbers)Try: number= Int (Input ("Please enter an integer->:") ) Collatz (number)exceptValueError:Print("Please input a integer number")

Python programming Quick Start-making tedious work automation chapter three function exercises and their answers

Related Article

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.