Python core programming, Version 2, 209th, Chapter 8 exercises-answers to Python core programming-self-developed-

Source: Internet
Author: User

8-1. Condition Statement. See the following code:

# Statement
If x> 0:
# Statement B
Pass
Elif x <0:
# Statement C
Pass
Else:
# Statement D
Pass
# Statement E

(A) If x <0, which of the preceding statements (A, B, C, D, E) will be executed?
(B) If x = 0, which of the above statements will be executed?
(C) If x> 0, which of the above statements will be executed?
[Answer]
(A) ACE
(B) ADE
(C) ABE
You can experiment with the Code as follows:

x = int(raw_input('Please input X ... '))print "Statement A"if x > 0:    print "Statement B"    passelif x < 0:    print "Statement C"    passelse:    print "Statement D"    passprint "Statement E"

8-2. Loop. Write a program that allows the user to input three numbers: (f) rom, (t) o, and (I) ncrement. Take I as the step and count from f to t, including f and t. For example, if the input is f = 2, t = 26, I = 4, the program will output 2, 6, 10, 14, 18, 22, 26.
[Answer]
The Code is as follows:

f = raw_input("Please input start number ... ")t = raw_input("Please input end number ... ")i = raw_input("please input step number ... ")for i in range(int(f), int(t) + 1, int(i)):    print i

 
8-3.range (). If we need to generate the following lists, which parameters need to be provided in the range () built-in function?
(A) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
(B) [3, 6, 9, 12, 15, 18]
(C) [-1, 20,200,420,640,860]
[Answer]
The Code is as follows:

>>> range(10)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> range(3,19,3)[3, 6, 9, 12, 15, 18]>>> range(-20,861,220)[-20, 200, 420, 640, 860]>>>

 

8-4. prime number. In this chapter, we have provided some code to determine the maximum approximate number of a number or whether it is a prime number. Convert the code into a function that returns a Boolean value. The function name is isprime (). If the input is a prime number, True is returned. Otherwise, False is returned.
[Answer]
The Code is as follows:
Def isprime (number): switch = True if number <= 1: switch = False for I in range (2, number/2 + 1): if number % I = 0: switch = False return switch number = raw_input ("Please input your number... ") print isprime (int (number ))

8-5. approx. Complete a function named getfactors. It accepts an integer as a parameter and returns a list of all its approx. values, including 1 and itself.
[Answer]
The Code is as follows:
def getfactors(number):    factorList = []    for i in range(1, number + 1):        if number % i == 0: factorList.append(i)    return factorList    number = raw_input("Please input your number ... ")print getfactors(int(number))
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.