質問編集履歴
3
コードにバグが有ったので追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,8 +8,15 @@
|
|
8
8
|
dic = {"test": None}
|
9
9
|
|
10
10
|
# このif文をきれいに書きたい
|
11
|
+
|
12
|
+
# dic['test']がNoneの場合エラーになる
|
11
13
|
if 'test' in dic and len(dic['test']) == 0 and dic['test'] is not None :
|
12
14
|
test = dic['test']
|
15
|
+
|
16
|
+
# 追記
|
17
|
+
if 'test' in dic and dic['test'] is not None and len(dic['test']) == 0 :
|
18
|
+
test = dic['test']
|
19
|
+
|
13
20
|
else:
|
14
21
|
print("error")
|
15
22
|
|
2
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
pythonで文字列が0のとき、Noneのとき、dict形式のkeyが無いときに引っ掛けるif文を作ったのですが、もう少し短く書く方法は無いでしょうか
|
2
2
|
|
3
3
|
```
|
4
|
-
#
|
4
|
+
# 以下のパターンを引っ掛けたい
|
5
5
|
dic = {"hello": "hello"}
|
6
6
|
dic = {"test": ""}
|
7
7
|
dic = {"test": " "}
|
1
誤字修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -10,6 +10,8 @@
|
|
10
10
|
# このif文をきれいに書きたい
|
11
11
|
if 'test' in dic and len(dic['test']) == 0 and dic['test'] is not None :
|
12
12
|
test = dic['test']
|
13
|
+
else:
|
14
|
+
print("error")
|
13
15
|
|
14
16
|
```
|
15
17
|
|