teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

コードにバグが有ったので追記

2019/04/13 07:13

投稿

cray123
cray123

スコア47

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

追記

2019/04/13 07:13

投稿

cray123
cray123

スコア47

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,7 @@
1
1
  pythonで文字列が0のとき、Noneのとき、dict形式のkeyが無いときに引っ掛けるif文を作ったのですが、もう少し短く書く方法は無いでしょうか
2
2
 
3
3
  ```
4
- # 想定してるdicのパターン
4
+ # 以下のパターンを引っ掛けたい
5
5
  dic = {"hello": "hello"}
6
6
  dic = {"test": ""}
7
7
  dic = {"test": " "}

1

誤字修正

2019/04/13 07:07

投稿

cray123
cray123

スコア47

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