【发布时间】:2019-02-08 20:26:10
【问题描述】:
def LongestWord(sen):
# first we remove non alphanumeric characters from the string
# using the translate function which deletes the specified characters
sen = sen.translate(None, "~!@#$%^&*()-_+={}[]:;'<>?/,.|`")
# now we separate the string into a list of words
arr = sen.split(" ")
print(arr)
# the list max function will return the element in arr
# with the longest length because we specify key=len
return max(arr, key=len)
**print LongestWord("Argument goes here")**
这条线有什么问题?我怎样才能改变它?我无法理解!这让我真的很不安,因为 Coderbyte.com 上说这是真的而且有效!
【问题讨论】:
-
您似乎是从函数本身调用函数。正确缩进
-
@shahidammer 不,我认为星号
**使它看起来缩进了,但没有它们它会在外部范围内 -
错误是什么?
-
您需要 Python 2.7 来运行此代码,而不是 3.x。 translate函数的参数nr发生了变化。
-
要使其适合 3.x,请查看:stackoverflow.com/questions/23175809/…
标签: python