[Python]小甲鱼Python视频第34课(with else)课后题及参考解答

# -*- coding: utf-8 -*-
"""
Created on Sun Feb 24 13:36:33 2019

@author: fengs
"""

"""
0. 在 Python 中,else 语句能跟哪些语句进行搭配?
    if
    while
    with
    except
"""

"""
1. 请问以下例子中,循环中的 break 语句会跳过 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)

    会跳过

"""

"""
2. 请目测以下代码会打印什么内容?
try:
        print('ABC')
except:
        print('DEF')
else:
        print('GHI')
finally:
        print('JKL')

ABC
GHI
JKL
"""


"""
3. 使用什么语句可以使你不比再担心文件打开后却忘了关闭的尴尬?
    
    with open as f:
        ....
        #f.close(); #不要写,with调用完成时会清理当前对象
        
    
"""


"""
4. 使用 with 语句固然方便,但如果出现异常的话,文件还会自动正常关闭吗?
可以,无论如何都会清理当前对象,文件还是会自动关闭
"""


"""
5. 你可以换一种形式写出下边的伪代码吗?
with A() as a:
    with B() as b:
        suite

方法1:
a = A()
if 'a' in locals():
    b = B()
    if 'b' in locals():
        suite
方法2:with 语句处理多个项目的时候,可以用逗号隔开写成一条语句
with A() as , B() as b:
    suite
    

"""


"""
动动手:
0. 使用 with 语句改写以下代码,让 Python 去关心文件的打开与关闭吧。
def file_compare(file1, file2):
    f1 = open(file1)
    f2 = open(file2)
    count = 0 # 统计行数
    differ = [] # 统计不一样的数量

    for line1 in f1:
        line2 = f2.readline()
        count += 1
        if line1 != line2:
            differ.append(count)

    f1.close()
    f2.close()
    return differ

file1 = input('请输入需要比较的头一个文件名:')
file2 = input('请输入需要比较的另一个文件名:')

differ = file_compare(file1, file2)

if len(differ) == 0:
    print('两个文件完全一样!')
else:
    print('两个文件共有【%d】处不同:' % len(differ))
    for each in differ:
        print('第 %d 行不一样' % each)

"""
"""
def file_compare(file1, file2):
    count = 0 # 统计行数
    differ = [] # 统计不一样的数量
    with open(file1) as f1, open(file2) as f2:
        for line1 in f1:
            line2 = f2.readline()
            count += 1
            if line1 != line2:
                differ.append(count)
    return differ

file1 = input('请输入需要比较的头一个文件名:')
file2 = input('请输入需要比较的另一个文件名:')

differ = file_compare(file1, file2)

if len(differ) == 0:
    print('两个文件完全一样!')
else:
    print('两个文件共有【%d】处不同:' % len(differ))
    for each in differ:
        print('第 %d 行不一样' % each)
        
"""        
"""
1. 你可以利用异常的原理,修改下面的代码使得更高效率的实现吗?
print('|--- 欢迎进入通讯录程序 ---|')
print('|--- 1:查询联系人资料  ---|')
print('|--- 2:插入新的联系人  ---|')
print('|--- 3:删除已有联系人  ---|')
print('|--- 4:退出通讯录程序  ---|')

contacts = dict()

while 1:
    instr = int(input('\n请输入相关的指令代码:'))
    
    if instr == 1:
        name = input('请输入联系人姓名:')
        if name in contacts:
            print(name + ' : ' + contacts[name])
        else:
            print('您输入的姓名不再通讯录中!')

    if instr == 2:
        name = input('请输入联系人姓名:')
        if name in contacts:
            print('您输入的姓名在通讯录中已存在 -->> ', end='')
            print(name + ' : ' + contacts[name])
            if input('是否修改用户资料(YES/NO):') == 'YES':
                contacts[name] = input('请输入用户联系电话:')
        else:
            contacts[name] = input('请输入用户联系电话:')

    if instr == 3:
        name = input('请输入联系人姓名:')
        if name in contacts:
            del(contacts[name])         # 也可以使用dict.pop()
        else:
            print('您输入的联系人不存在。')
            
    if instr == 4:
        break

print('|--- 感谢使用通讯录程序 ---|')

"""

print('|--- 欢迎进入通讯录程序 ---|')
print('|--- 1:查询联系人资料  ---|')
print('|--- 2:插入新的联系人  ---|')
print('|--- 3:删除已有联系人  ---|')
print('|--- 4:退出通讯录程序  ---|')

contacts = dict()
while True:
    instr = int(input('\n请输入相关的指令代码:'))
    if instr == 1:
        name = input('请输入联系人姓名:')
        #if name in contacts:
        try:
            print(name + ' : ' + contacts[name])
        except(KeyError):
        #else:
            print('您输入的姓名不再通讯录中!')
    if instr == 2:
        name = input('请输入联系人姓名:')
        #if name in contacts:
        try:
            print('您输入的姓名在通讯录中已存在 -->> ', end='')
            print(name + ' : ' + contacts[name])
            if input('是否修改用户资料(YES/NO):') == 'YES':
                contacts[name] = input('请输入用户联系电话:')
        except(KeyError):
        #else:
            contacts[name] = input('请输入用户联系电话:')
    if instr == 3:
        name = input('请输入联系人姓名:')
        #if name in contacts:
        try:
            del(contacts[name])         # 也可以使用dict.pop()
        #else:
        except(KeyError):
            print('您输入的联系人不存在。')
    if instr == 4:
        break

print('|--- 感谢使用通讯录程序 ---|')

  

猜你喜欢

转载自www.cnblogs.com/alimy/p/10426027.html