• python基础学习(九)


    19.解包

    # 解包 unpacking
    user1 = ["张三", 21, "1999.1.1"]
    # tuple 类型
    user2 = ("李四", 21, "1999.10.1")
    
    # user_name = user[0]
    # user_age = user[1]
    # user_bir = user[2]
    
    # 更简单的方式把列表元素 附给变量
    user_name1, user_age1, user_bir1 = user1
    # age和name换了位置  仔细看结果!!!
    user_age2, user_name2, user_bir2 = user2
    
    
    print("name1:"+user_name1)
    print("age1:" + str(user_age1))
    print("bir1:"+user_bir1)
    print("-----------帅气分割线-----------------")
    print("name2:"+str(user_name2))
    print("age2:"+user_age2)
    print("bir2:"+user_bir2)
    print("李四的名字和age 位置反了 这说明是按照顺序来赋值的")

    run结果:

     20.异常处理

    错误总结版本:

    # 异常 exception   运行时候检测到的错误为 异常
    # 错误 error
    
    # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 5
    # if True
    #       ^
    # SyntaxError: invalid syntax
    if True
        print("Hello SongKe")
    
    # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 13
    #    print("Hello SongKe1")
    #        ^
    # IndentationError: expected an indented block
    if True:
    print("Hello SongKe1")
    
    # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 19
    #     print(str_hi)
    #     ^
    # IndentationError: unexpected indent
    str_hi = "Hello SongKe2"
        print(str_hi)
    
    # 请输入num:松柯
    # Traceback (most recent call last):
    #   File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 26, in <module>
    #     num = int(input("请输入num:"))
    # ValueError: invalid literal for int() with base 10: '松柯'
    num1 = int(input("请输入num1:"))
    print(num1)
    
    # 包起来 可能出现异常的代码
    try:
        num2 = int(input("请输入num2:"))
        print(num2)
    except:
        print("error input")
    
        # Traceback (most recent call last):
        #   File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 40, in <module>
        #     num = 1/0
        # ZeroDivisionError: division by zero
        # 分母不能为0
        # num = 1 / 0
    
    # 当输入0 时候 不知道是字符串转换引起的还是 输入0引起的异常  还需要能处理不同情况的
    # 请输入num3:0
    # division by zero
    # -------------------------------------------------
    # 请输入num3:松柯
    # invalid literal for int() with base 10: '松柯'
    try:
        num3 = int(input("请输入num3:"))
        result = 1 / num3
        print(result)
        # ZeroDivisionError 分母为0的异
    except ZeroDivisionError as error:
        print(error)
        # ValueError 输入异常
    except ValueError as error:
        print(error)
    
    
    def test_fails(num4):
        return 1 / num4
    
    
    try:
        num4 = int(input("请输入num4:"))
        result1 = test_fails(num4)
        print(result1)
        # ZeroDivisionError 分母为0的异常
    except ZeroDivisionError as error:
        print(error)
        # ValueError 输入异常
    except ValueError as error:
        print(error)

    正常版本:

    # 异常 exception   运行时候检测到的错误为 异常
    # 错误 error
    
    # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 5
    # if True
    #       ^
    # SyntaxError: invalid syntax
    if True:
        print("Hello SongKe")
    
    # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 13
    #    print("Hello SongKe1")
    #        ^
    # IndentationError: expected an indented block
    if True:
        print("Hello SongKe1")
    
    # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 19
    #     print(str_hi)
    #     ^
    # IndentationError: unexpected indent
    str_hi = "Hello SongKe2"
    print(str_hi)
    
    # 请输入num:松柯
    # Traceback (most recent call last):
    #   File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 26, in <module>
    #     num = int(input("请输入num:"))
    # ValueError: invalid literal for int() with base 10: '松柯'
    num1 = int(input("请输入num1:"))
    print(num1)
    
    # 包起来 可能出现异常的代码
    try:
        num2 = int(input("请输入num2:"))
        print(num2)
    except:
        print("error input")
    
        # Traceback (most recent call last):
        #   File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 40, in <module>
        #     num = 1/0
        # ZeroDivisionError: division by zero
        # 分母不能为0
        # num = 1 / 0
    
    # 当输入0 时候 不知道是字符串转换引起的还是 输入0引起的异常  还需要能处理不同情况的
    # 请输入num3:0
    # division by zero
    # -------------------------------------------------
    # 请输入num3:松柯
    # invalid literal for int() with base 10: '松柯'
    try:
        num3 = int(input("请输入num3:"))
        result = 1 / num3
        print(result)
        # ZeroDivisionError 分母为0的异
    except ZeroDivisionError as error:
        print(error)
        # ValueError 输入异常
    except ValueError as error:
        print(error)
    
    
    def test_fails(num4):
        return 1 / num4
    
    
    try:
        num4 = int(input("请输入num4:"))
        result1 = test_fails(num4)
        print(result1)
        # ZeroDivisionError 分母为0的异常
    except ZeroDivisionError as error:
        print(error)
        # ValueError 输入异常
    except ValueError as error:
        print(error)

    run结果: 

  • 相关阅读:
    Adjacent Bit Counts(uvalive)
    UVALIVE 4556 The Next Permutation
    vector(实现存图)
    最大连续子序列和(模板)
    全选和反选
    .netCore上传图片,要用FormFileCollection ,不能用List
    .NET-Core中 HttpContext.Response.Write() 中文输出乱码
    Core中Cookie和Session的新用法
    Ajax反填
    复选框变成单选
  • 原文地址:https://www.cnblogs.com/songxiaoke/p/11883903.html
Copyright © 2020-2023  润新知