034 丰富的else语句及简洁的with语句
- 丰富的else语句
要么怎样,要么不怎样
干完了能怎样,干不完就别想怎样
没有问题,那就干吧
求最大约数
def showMaxFactor(num):
count=num // 2
while count>1:
if num%count==0:
print('%d最大的约数是%d'%(num,count))
break
count-=1
else:
print('%d是素数!'% num)
num=int(input('请输入一个数:'))
showMaxFactor(num)
try:
print(int('123'))
except ValueError as reason:
print('出错啦:'+str(reason))
else:
print('没有任何异常!')
- 简洁的with语句
try:
with open('data.txt','w') as f:
for each_line in f:
print(each_line)
except OSError as reason:
print('出错啦:'+ str(reason))