Hướng dẫn how do i skip a keyerror in python? - làm cách nào để bỏ qua lỗi bàn phím trong python?

Bỏ qua một ngoại lệ keyerror trong python #

Sử dụng khối try/except để bỏ qua ngoại lệ KeyError trong Python. Khối except sẽ chỉ chạy nếu ngoại lệ KeyError được nâng lên trong khối try. Bạn có thể sử dụng từ khóa

Copied!

my_dict = {'name': 'Alice'} print[my_dict.get['age']] # 👉️ None print[my_dict.get['age', 'default value']] # 👉️ 'default value'
0 để bỏ qua ngoại lệ.

Copied!

my_dict = {'name': 'Alice'} try: print[my_dict['age']] except KeyError: # 👇️ this runs pass # 👇️ you can use continue instead of pass # if you are in a for loop and want to skip to next iteration for i in range[3]: try: print[my_dict['age']] except KeyError: # 👇️ this runs continue

Nếu bạn chỉ muốn bỏ qua ngoại lệ KeyError nếu nó được ném, hãy sử dụng khối try/except.

Khóa

Copied!

my_dict = {'name': 'Alice'} print[my_dict.get['age']] # 👉️ None print[my_dict.get['age', 'default value']] # 👉️ 'default value'
3 không có trong từ điển, do đó, KeyError được nâng lên và khối except của chúng tôi chạy.

Bạn có thể sử dụng từ khóa

Copied!

my_dict = {'name': 'Alice'} print[my_dict.get['age']] # 👉️ None print[my_dict.get['age', 'default value']] # 👉️ 'default value'
0 nếu bạn không muốn xử lý lỗi theo bất kỳ cách nào.

Nếu bạn đang ở trong vòng lặp

Copied!

my_dict = {'name': 'Alice'} print[my_dict.get['age']] # 👉️ None print[my_dict.get['age', 'default value']] # 👉️ 'default value'
7 và muốn bỏ qua lần lặp tiếp theo, hãy sử dụng từ khóa

Copied!

my_dict = {'name': 'Alice'} print[my_dict.get['age']] # 👉️ None print[my_dict.get['age', 'default value']] # 👉️ 'default value'
8 thay thế.

Nếu bạn chỉ cần truy cập một khóa cụ thể và cần bỏ qua ngoại lệ KeyError, hãy sử dụng phương thức

Copied!

my_dict = {'name': 'Alice'} if 'age' in my_dict: print[my_dict['age']] else: # 👇️ this runs print['key is not present in the dict']
0.

Copied!

my_dict = {'name': 'Alice'} print[my_dict.get['age']] # 👉️ None print[my_dict.get['age', 'default value']] # 👉️ 'default value'

Phương thức Dict.get trả về giá trị cho khóa đã cho nếu khóa nằm trong từ điển, nếu không, giá trị mặc định được trả về.

Phương thức lấy 2 đối số sau:

TênSự mô tả
Chìa khóaKhóa để trả về giá trị
mặc địnhGiá trị mặc định được trả về nếu khóa được cung cấp không có trong từ điển [tùy chọn]

Nếu một giá trị cho tham số

Copied!

my_dict = {'name': 'Alice'} if 'age' in my_dict: print[my_dict['age']] else: # 👇️ this runs print['key is not present in the dict']
1 không được cung cấp, nó mặc định là

Copied!

my_dict = {'name': 'Alice'} if 'age' in my_dict: print[my_dict['age']] else: # 👇️ this runs print['key is not present in the dict']
2, do đó phương thức

Copied!

my_dict = {'name': 'Alice'} if 'age' in my_dict: print[my_dict['age']] else: # 👇️ this runs print['key is not present in the dict']
3 không bao giờ tăng KeyError.

Một cách phổ biến khác để tránh ngoại lệ KeyError là sử dụng toán tử

Copied!

my_dict = {'name': 'Alice'} if 'age' in my_dict: print[my_dict['age']] else: # 👇️ this runs print['key is not present in the dict']
6 và kiểm tra xem khóa có có trong từ điển trước khi truy cập không.

Copied!

my_dict = {'name': 'Alice'} if 'age' in my_dict: print[my_dict['age']] else: # 👇️ this runs print['key is not present in the dict']

Các thử nghiệm trong nhà điều hành để thành viên. Ví dụ,

Copied!

my_dict = {'name': 'Alice'} if 'age' in my_dict: print[my_dict['age']] else: # 👇️ this runs print['key is not present in the dict']
7 đánh giá thành

Copied!

my_dict = {'name': 'Alice'} if 'age' in my_dict: print[my_dict['age']] else: # 👇️ this runs print['key is not present in the dict']
8 nếu

Copied!

my_dict = {'name': 'Alice'} if 'age' in my_dict: print[my_dict['age']] else: # 👇️ this runs print['key is not present in the dict']
9 là thành viên của
from shares import EXCHANGE_DATA
portfolio_str=input["Please list portfolio: "]
portfolio_str= portfolio_str.replace[' ','']
portfolio_str= portfolio_str.upper[]
portfolio_list= portfolio_str.split[',']
print[]
print['{:

Bài Viết Liên Quan

Chủ Đề