|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Gabber 于 2017-9-19 23:59 编辑
else语句:
- while / for
- 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:
- a = int(input('请输入一个整数:'))
- except ValueError as reason:
- print('出错啦:' + str(reason))
- else:
- print('没有任何异常!您输入的数字是:%d' % a)
复制代码
with 语句
- try:
- with open('data.txt','w') as f:
- for each in f:
- print(each)
- except OSError as reason:
- print('出错了:' + str(reason))
复制代码
其它格式:
- f1 = open(file1)
- f2 = open(file2)
- ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
- with open(file1)as f1,open(file2) as f2
复制代码 |
|