[英]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.