[問題] list
def my_problem4_1(a):
if a==1:
return "one"
elif a==2:
return "two"
elif a==3:
return "three"
else:
return "larger than three"
Q: 不要用 if-else statement, 改用 list 來達到相同的功能
我:
def my_problem4_1(a):
listA=['one','two','three','larger than three']
if a>4:
print listA[3]
else:
print listA[a-1]
可是這樣還是用到 if-else 怎麼樣可以直接用list達到相同功能
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.44.123.182
推
12/06 19:13, , 1F
12/06 19:13, 1F
推
12/06 19:17, , 2F
12/06 19:17, 2F
推
12/06 19:18, , 3F
12/06 19:18, 3F
→
12/06 19:20, , 4F
12/06 19:20, 4F
→
12/06 19:38, , 5F
12/06 19:38, 5F
→
12/06 19:47, , 6F
12/06 19:47, 6F
討論串 (同標題文章)
Python 近期熱門文章
PTT數位生活區 即時熱門文章