python基礎學習(九)

19.解包python

# 解包 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結果:spa

 

 20.異常處理code

錯誤總結版本:blog

# 異常 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結果: input

相關文章
相關標籤/搜索