第七章课后练习

1

str_n = input('请输入整数N: ')
try:
    n = int(str_n)
    print(n)
    i = 0
    while True:
        try:
            a , b = input('请输入2个整数(空格隔开): ').split()
            print(int(a) // int(b))
            i += 1
            if i >= n: break
        except:
            print('务必输入空格隔开的2个整数!')
except:
    print('请输入整数N!')

2

while True:
    str_n = input('请输入整数N: ')
    if str_n == 'exit':
        import sys
        sys.exit(0)
    try:
        n = int(str_n)
        if n % 2 != 0:
            print('有趣')
        elif 5 > n > 2:
            print('没意思')
        elif 20 > n > 6:
            print('有趣')
        else:
            print('没意思')
    except:
        print('务必输入整数')

3

def fn(tp):
    for e in tp:
        if not isinstance(e, str):
            raise ValueError('所有元素必须是字符串')
        if not (20 >= len(e) >= 6):
            raise ValueError('字符串的长度必须在6~20之间')
    print(tp)
    
if __name__ == '__main__':
    fn(('fkjava', 'crazyit'))
#    fn((20,))
    fn(('fkjavafkjavafkjavafkjava'))

4

while True:
    st = input("请输入3个点的x、y值(空格隔开): ")
    if st == 'exit':
        import sys
        sys.exit(0)
    try:
        x1_st, y1_st, x2_st, y2_st, x3_st, y3_st = st.split()
        x1, y1, x2, y2, x3, y3 = float(x1_st), float(y1_st), float(x2_st), float(y2_st), float(x3_st), float(y3_st)
        if x1 == 0 and x2 == 0 and x3 == 0:
            print('处于同一条直线')
        elif 0 in (x1, x2, x3):
            print('不处于同一条直线')
        elif y1 / x1 == y2 / x2 and y1 / x1 == y3 / x3:
            print('处于同一条直线')
        else:
            print('不处于同一条直线')
    except:
        print('必须输入6个空格隔开的数')
    
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 点我我会动 设计师:白松林 返回首页