iT邦幫忙

0

[我很爛]SoloLearn|Python 題目

完全沒看過的語法,也不知該如何查起QwQ
來這裡許願,希望有人願意指點我迷津⋯⋯

Fill in the blanks to print the first elements of the list, if it contains even number of elements.
翻譯:求list的第一個偶數

list = [1, 2, 3, 4]
if ___ (list) % 2 == 0 ___
print(list[ ___ ] )

依我看來第二個空格(==0後面那個)是”:”,因為只佔一格。
“if”後面填三個字,list 裡只能填一個。

結論是我英文很爛。XD
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
4
listennn08
iT邦高手 5 級 ‧ 2020-09-30 22:09:24
最佳解答

Fill in the blanks to print the first elements of the list, if it contains even number of elements.
翻譯 > 填充空格印出第一個元素,如果 list 包含偶數個元素的話

list = [1, 2, 3, 4]
if len(list) % 2 == 0:
    print(list[0])

我正要打呢...感謝尼OuO

應該要給 Tony.Ko 大大才對 他比較快XD

1
japhenchen
iT邦超人 1 級 ‧ 2020-09-30 21:28:32

先想到的

list = [1, 2, 3, 4]
s =[ x for x in list if x % 2 == 0 ]
print(s[0])

另一種

list = [1, 2, 3, 4]
m = next( i for i in list if i % 2 == 0 )
print(m)

他格式是規定的,只能填空格,我也有想過是那種單行的寫法,但是他結構沒有給for,在if後面自己加雖然剛好三個字,但這樣最後一行不知道該print甚麼。
管他的,硬改:

list = [1, 2, 3, 4]
if for (list) % 2 == 0:
print(list[0])

輸出爆錯,line 2:
if for (list) % 2 == 0:
SyntaxError: invalid syntax

2
Tony.Ko
iT邦新手 5 級 ‧ 2020-09-30 22:07:59

Fill in the blanks to print the first elements of the list, if it contains even number of elements.
正確翻譯:
如果list有偶數個元素,print出list裡的第一個元素

三個空格分別是 len, :, 0

https://www.sololearn.com/Discuss/139311/fill-in-the-blanks-to-print-the-first-element-of-the-list-if-it-contains-even-number-of-elements

原來是英文的部分啊...

Tony.Ko iT邦新手 5 級 ‧ 2020-09-30 22:13:31 檢舉

Sololearn的問題答案通常就是該章節所教的語法
如果還是想不出來,可以看使用者的討論,通常也會有正確答案在裡頭

0
iT邦新手 4 級 ‧ 2020-09-30 22:16:31

是不是誤會題目了!?看起來是檢查list有幾個元素

0
8500718
iT邦新手 5 級 ‧ 2020-10-04 00:03:51

小弟只會用for來叫出list裡面的值 太嫩了 @@

我要發表回答

立即登入回答