马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 游戏小虾米 于 2017-7-20 23:16 编辑
Tip:
一,理论
else可以和while连用
else可以和try连用
with可以和open连用,不用考虑close()
二,应用
1def 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)
2try:
num = int('abc')
except ValueError as reason:
print('有错误,' + str(reason))
else:
print('没问题')
有错误,invalid literal for int() with base 10: 'abc'
3try:
num = int(123)
except ValueError as reason:
print('有错误,' + str(reason))
else:
print('没问题')
没问题
4try:
with open('data.txt', 'w') as f:
for each_line in f:
print(each_line)
except OSError as reason:
print('有错误,' + str(reason))
三,课后练习 |