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))