# 《Python入门经典 以解决计算问题为导向的Python编程实践》Lesson2

5+3>9-2

Python支持这样的比较：
aInt = 5
0<=aInt<=5

while booleanExpression:
#suite 1
#else:
#suite2

1.

#-*-coding:gbk-*-
for i in range(100,1000):
if i%17==0:
print i,

2.

#-coding:gbk-*-
#(a)
Sum=0
while myNum>=1:
Sum+=myNum
myNum-=1
print Sum
#(b)
for i in range(1,myNum+1):
Sum=0
Kong=0
Str=""
for j in range(1,i+1):
Sum+=j
if j==i:
Str+=str(j)+" "*((myNum-Kong)*2-1)
else:
Str+=str(j)+"+"
Kong+=1
print Str,"=",Sum
print
#(c)
for i in range(1,myNum+1):
Sum=0
Kong=0
Str=""
for j in range(1,i+1):
Sum+=j
if j==i:
Str+=str(j)+" "*((myNum-Kong)*2-1)
else:
Str+=str(j)+"+"
Kong+=1
if Sum%i==0:
print Str,"=",Sum
print

3.

#-*-coding:gbk-*-
#method_1 从后往前输出
Money=int(MoneyStr)
if Money/29!=0:
if Money%29!=0:
print Money%29,"纳特",
Money=Money/29
if Money/17!=0:
if Money%17!=0:
print Money%17,"锡可",
Money=Money/17
print Money,"加仑"
else:
print Money,"锡可"
else:
if Money!=0:
print Money,"纳特"
#method_2
x=int(MoneyStr)
if x/(29*17)!=0:#money数是否大于或等于29*17的整数倍，有加仑
print x/(29*17),"加仑",
x=x%(29*17)#得到加仑后的余数

if x/29!=0:#money数是否大于或等于29的整数倍，有锡可
print x/29,"锡可",#输出锡可
if x%29!=0:#有锡可后的余数
print x%29,"纳特"
else:#money凑不够一个锡可，非零时输出纳特
if x!=0:
print x,"纳特"

4.

#-*-coding:gbk-*-
#当因数加到一半就大于输入的数字，break
#SumofDivisor预先就设为1，Divisor就是2，少算一步
#更多详情请参考“梅森素数”
theNum=2
Deficient=0
Abundant=0
while theNum<=topNum:
#print theNum
Divisor=2
SumofDivisor=1
while Divisor<theNum:#不包括数字本身
if theNum%Divisor==0:
SumofDivisor+=Divisor
if SumofDivisor>theNum:
break
Divisor+=1
if SumofDivisor==theNum:
print theNum,
elif SumofDivisor>theNum:
Abundant+=1
else:
Deficient+=1
theNum+=1
print
print Abundant,"are abundant."
print Deficient,"are deficient."

5.

#-*-coding:gbk-*-
h=float(h)
w=float(w)
bmi=w/h**2
if bmi>=30.0:
print "Too fat"
elif 25.0<=bmi<=29.9:
print "Overweight"
elif 18.5<bmi<=24.9:
print "Normal"
elif bmi<=18.5:
print "Too light"

6.

#-*-coding:gbk-*-
Int=raw_input("Input an integer:")
while not Int.isdigit():
Int=raw_input("Error: try again. Input an integer:")
Int=int(Int)
print "The integer is :",Int

7.

#-*-coding:gbk-*-
#1)
for i in range(1,10):
Str=""
for j in range(1,i+1):
Str+=str(j)
#print Str
print Str,"*",8,"+",i,"=",int(Str)*8+i
#2)
for i in range(1,10):
Str=""
for j in range(1,i+1):
Str+=str(j)
#print Str
print Str,"*",9,"+",i+1,"=",int(Str)*9+(i+1)
#3)
for i in range(9,1,-1):
Str=""
for j in range(9,i-1,-1):
Str+=str(j)
#print Str
print Str,"*",9,"+",i-2,"=",int(Str)*9+(i-2)
#4)
for i in range(1,10):
Str=""
for j in range(1,i+1):
Str+=str(1)
#print Str
print Str,"*",Str,"=",int(Str)**2

8.

#-*-coding:gbk-*-
import math

i=2
while i<=math.sqrt(myNum):
if myNum%i==0:
print myNum,"is not a prime."
break
i+=1
else:
print myNum,"is a prime."

9.

#-*-coding:gbk-*-
topNum=raw_input("upper limit:")
topNum=int(topNum)
for theNum in range(2,topNum+1):
sumOfDivisors=0
for divisor in range(1,theNum):
if theNum%divisor==0:
sumOfDivisors+=divisor
if sumOfDivisors>theNum:
break
if theNum==sumOfDivisors:
print theNum,"is perfect."
elif theNum<sumOfDivisors:
print theNum,"is abundant."
else:
print theNum,"is deficient."

10.

#-*-coding:gbk-*-
import math
#(a)#(b)
Expr="ax^2+bx+c"
print Expr
a=float(raw_input("a:"))
b=float(raw_input("b:"))
c=float(raw_input("c:"))
delta=b**2-4*a*c
if delta==0:
res=(-b+math.sqrt(delta))/(2*a)
print Expr,"has one root."
print "It is",res
elif delta>0:
res1=(-b+math.sqrt(delta))/(2*a)
res2=(-b-math.sqrt(delta))/(2*a)
print Expr,"has two roots."
print "They are",res1,res2
else:
part1=math.sqrt(math.fabs(delta))
part2=-math.sqrt(math.fabs(delta))
x=-b/2*a
y=part1/2*a
res1=x+eval(str(y)+"j")
res2=x-eval(str(y)+"j")
print Expr,"has two imaginary roots."
print "They are",res1,res2

11.

(a)

myVar=1,3,2,12
(b)
{x|x=2*i+1}(i是除了1以外的任意整数,i!=1)
{3}
{x|x=2*i}(i是小于等于5的任意整数,i<=5)
{x|x=2*i}(i是大于5的任意整数,i>5)

12.

#-*-coding:gbk-*-
Str=raw_input("Input a string:")
s=""
for i in range(len(Str)):
if (i+1)%3==0:s+=Str[i]
print s

13.略

14.is看id, ==看值

15.在第6行与第7行之间插入"i-=1",否则死循环

16.在while循环体内插入if...break

17.

#-*-coding:gbk-*-
import math
#初级版
#Num=raw_input("Enter an integer greater than 2:")
#while Num.isdigit():
#    Num=raw_input("Error. It is not an interger. Try again:")
#Num=int(Num)
#while Num<=2:
#    Num=raw_input("Error. Integer should be greater than 2. Try again:")
#i=0
#while Num>=2:
#    Num=math.sqrt(Num)
#    i+=1
#    print("%d: %.3f" % (i,Num))

#高级版
#用函数什么的最方便了=。=
def IfInputValid(x):
try:
if int(x)>2:
return x
else:
print "Integer less than 2!"
return 0

except:
print "Not an integer!!!"
return 0

Num=raw_input("Enter an integer greater than 2:")
while not IfInputValid(Num):
Num=raw_input("Enter an integer greater than 2:")
Num=int(Num)
i=0
while Num>=2:
Num=math.sqrt(Num)
i+=1
print("%d: %.3f" % (i,Num))

18.略

19.(c)

20.略

21.True;False

22.迭代器是访问集合内元素的一种方式。迭代器对象从集合的第一个元素开始访问，直到所有的元素都被访问一遍后结束。e.g. range(); it=iter(lst), it.nest()

23.continue忽略循环后面的语句，直接跳到下一回的循环；break，直接跳出循环体

24.

#-*-coding:gbk-*-
#检查一个数是否是回文数
def IfPalindrome(x):
l=len(x)
ok=1
for i in range(int(l/2.)):
if x[i]!=x[l-i-1]:
ok=0
break
return ok

x=10000
while x<999999:
if IfPalindrome(str(x)[-4:]):
Num=x
y=x+1
if IfPalindrome(str(y)[-5:]):
more=2
while more<=10:
z=y+more
if IfPalindrome(str(z)[1:-1]):
break
more+=1
k=z+3
if len(str(k))==6:
if IfPalindrome(str(k)):
print Num,more
x+=1

#后面的数字指的是“超过3英里”时所走的实际英里数

25.

#-*-coding:gbk-*-
for num2 in range(10,100):
num3=num2**2
if 100<=num3<1000 and str(num3)[-2:]==str(num2):
print num2

26.

#-*-coding:gbk-*-
S,E,N,D,M,O,R,Y=1,0,0,0,1,0,0,0
for s in range(1,10):
S=s
for e in range(0,10):
E=e
for n in range(0,10):
N=n
for d in range(0,10):
D=d
M=1#逻辑推理M必为1
for o in range(0,10):
O=o
for r in range(0,10):
R=r
for y in range(0,10):
Y=y
#print S,E,N,D,M,O,R,Y
send=int(str(S)+str(E)+str(N)+str(D))
more=int(str(M)+str(O)+str(R)+str(E))
money=int(str(M)+str(O)+str(N)+str(E)+str(Y))
if send+more==money and len(set([S,E,N,D,M,O,R,Y]))==8:
print send,"+",more,"=",money

02-22 3584

07-10 67
08-12 116
04-29 3189
09-24 1062
12-21 2万+
12-23 1万+
06-24 70
06-06
03-30
11-01
01-27