5+3>9-2
就是8>7,python是先计算后比较
其他算术运算先于布尔运算符执行
就是8>7,python是先计算后比较
其他算术运算先于布尔运算符执行
Python支持这样的比较:
aInt = 5
0<=aInt<=5
while booleanExpression:
#suite 1
#else:
#suite2
当while循环布尔表达式为假时,进入else语句。注意:while内部的break跳出循环也会跳过else语句。
习题 2
1.
#-*-coding:gbk-*-
for i in range(100,1000):
if i%17==0:
print i,
2.
#-coding:gbk-*-
#(a)
myNum=int(raw_input("Please input a number:"))
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 从后往前输出
MoneyStr=raw_input("Input your money to convert(纳特):")
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/