此解答因為限於排版捡絮,所以很多縮排是有誤的,得再去此網(wǎng)址深入查詢答案莲镣,
ex8-1
(a) statement C
(b) statement D
(c) statement B
ex8-2
from_value=int(input("(f)rom:"))
to_value=int(input("(t)o:"))
increment_value=int(input("(i)ncrement:"))
for num in range(from_value,to_value,increment_value):
print num
ex8-3
print 'cList',cList
aList=[]
bList=[]
cList=[]
for i in range(10):
aList.append(i)
for i in range(3,19,3):
bList.append(i)
for i in range(-20,861,220):
cList.append(i)
print'aList',aList
print'bList',bList
ex8-4
def isprime(num):
count=num/2
while count>1:
if num%count==0:
return True
break
count-=1
else:
returnFalse
for i in range(100,200):
print i,isprime(i)
ex8-5
def getfactors(num):
aList=[]
count=num/2
while count>1:
if num%count==0:
aList.append(count)
count-=1
count-=1
return sorted(aList)
for i in range(20,40):
print i,':',getfactors(i)
ex8-6
if num%i==0:
#coding=utf-8
def get_prime_factors(num):
return[i for i in range(1, num+1) if num%i==0 and isprime(i)]
def isprime(num):
if num<=1:
return False
count=num/2
while count>1:
if num%count==0:
return False
count-=1
else:
returnTrue
def getfactors(num):
aList=[]
if isprime(num) or num<=1:
aList=[1,num]
return aList
factors=get_prime_factors(num)
while num not in factors:
for i in range(2,(num/2)+1):
num=num/i
aList.append(i)
break
else:
aList.append(num)
return aList
for i in range(1,30):
print getfactors(i)
ex8-7
def get_factors_no_self(num):
return[i for i in range(1, num)if num%i==0]
def is_perfect(num):
if num==sum(get_factors_no_self(num)):
return1
else:
return0
fori in range(1,30):
print i,is_perfect(i)
ex8-8
def getvalue(num):
count=num
total=1
while count>=1:
total=total*num
num=num-1
count-=1
returntotal
for i in range(1,11):
print i,getvalue(i)
ex9-9
def fibibonacci(n):
if n==1 or n==2:
return1
else:
return fibibonacci(n-1)+fibibonacci(n-2)
for i in range(1,100):
printi,fibibonacci(i)
ex8-10
#coding=utf-8
vowels='aeiou'
cosonants='bcdfghjklmnpqrstvwxyz'
def stat(sentence):
word_num=len(sentence.lower().strip().split(""))
vowels_num=0
cosonants_num=0
for c in sentence:
if c in vowels:
vowels_num+=1
elif c in cosonants:
cosonants_num+=1
else:
pass
return(word_num,vowels_num,cosonants_num)
def main():
sentence='Hello i am Bryan iiiI!'
print sentence
print"There are %d words,%d vowels and %d consonants."%stat(sentence)
if__name__=='__main__':
main()