![](/img/trans.png)
[英]Write a function that prints the first word after the first space in a string
[英]Write a function named first_word that returns the first word in a string
我的朋友給我發了一張截圖給我,他被困在評估中,問我是否能提供幫助。 所以我試了一下,現在已經兩天了,這一直困擾着我的夢想。
問題:“創建一個名為 first_word 的 function,它接受一個字符串並返回第一個單詞。
給定代碼:
assert first_word("Random string here") == "Random"
assert first_word("Another random string here") == "Another"
assert first_word("Again a random string... etc.") == "Again"
print("Excerscise is complete"
我在其他變體中嘗試過的:
def first_word(x):
print(first_word.split().pop(0))
return first_word
assert first_word("Random string here") == "Random"
assert first_word("Another random string here") == "Another"
assert first_word("Again a random string... etc.") == "Again"
print("Excerscise is complete"
def first_word(x):
print(first_word.split()
return
assert first_word("Random string here") == "Random"
assert first_word("Another random string here") == "Another"
assert first_word("Again a random string... etc.") == "Again"
print("Excerscise is complete"
我嘗試了很多不同的組合,但無法考慮將它們全部添加,我在網上查找過,但似乎無法使代碼按照評估的方式工作。 我可以返回第一個字母,但我無法全神貫注於如何獲得第一個單詞。
我朋友做的這個考核對我沒有實際影響,而且已經通過了,他不會有任何收獲。 我只想了解如何完成這項任務,這將幫助我弄清楚失敗的原因。
我目前在 eDiscovery 工作,正在努力獲得我的安全 + 和 OSCP。 我覺得獲得 python 的任何知識都是有益的。 任何幫助,將不勝感激。 非常感謝你
-Oerml
你在這里有正確的基本想法:
def first_word(x):
print(first_word.split().pop(0))
return first_word
但對語法感到困惑:
first_word
是function , x
是您要獲取第一個單詞的字符串參數。 first_word.split()
沒有意義,因為你不能split()
a function。你可以split()
是x
。print
和return
。 您的 function 應該return
第一個單詞(不是first_word
,它是 function 本身,而是通過拆分x
得到的字符串),而不是print
它。這應該工作:
def first_word(x):
return x.split()[0]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.