简体   繁体   English

编写一个名为 first_word 的 function 返回字符串中的第一个单词

[英]Write a function named first_word that returns the first word in a string

My buddy sent me a screenshot a question he was stuck on for an assessment and asked if I was able to help.我的朋友给我发了一张截图给我,他被困在评估中,问我是否能提供帮助。 So I took a shot at it and it's been two days now and this is haunting my dreams.所以我试了一下,现在已经两天了,这一直困扰着我的梦想。

Question: "create a function named first_word that takes in a string and returns the first word.问题:“创建一个名为 first_word 的 function,它接受一个字符串并返回第一个单词。

Given Code:给定代码:

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"

What I have Tried amongst other variations:我在其他变体中尝试过的:

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"

I have tried a lot of different combinations and can't think to add them all, I have looked up online and I can't seem to make the code work how it was for the assessment.我尝试了很多不同的组合,但无法考虑将它们全部添加,我在网上查找过,但似乎无法使代码按照评估的方式工作。 I can return the first letter but I cannot wrap my brain around how to get the first word.我可以返回第一个字母,但我无法全神贯注于如何获得第一个单词。

This assessment my friend is doing has no actual impact on me, and it's already passed so he will gain nothing from this.我朋友做的这个考核对我没有实际影响,而且已经通过了,他不会有任何收获。 I merely want to understand how to complete this task that will help me figure out why it is failing.我只想了解如何完成这项任务,这将帮助我弄清楚失败的原因。

I currently work in eDiscovery and am working towards getting my security + and OSCP.我目前在 eDiscovery 工作,正在努力获得我的安全 + 和 OSCP。 I feel gaining any knowledge of python will be beneficial.我觉得获得 python 的任何知识都是有益的。 Any help would be appreciated.任何帮助,将不胜感激。 Thank you very much非常感谢你

-Oerml -Oerml

You have the right basic idea here:你在这里有正确的基本想法:

def first_word(x):
    print(first_word.split().pop(0))
    return first_word

but are confused about the syntax:但对语法感到困惑:

  • first_word is the function , and x is the string parameter that you're trying to get the first word of. first_wordfunctionx是您要获取第一个单词的字符串参数 first_word.split() doesn't make sense, because you can't split() a function. What you can split() is x . first_word.split()没有意义,因为你不能split() a function。你可以split()x
  • You're mixing up print and return .你混淆了printreturn Your function is supposed to return the first word (not first_word , which is the function itself, but the string you get by splitting x ), and not print it.您的 function 应该return第一个单词(不是first_word ,它是 function 本身,而是通过拆分x得到的字符串),而不是print它。

This should work:这应该工作:

def first_word(x):
    return x.split()[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM