Python 3.x 当我使用string时,在另一个def中find[::],string.find()表示它';s属性不';行不通
当我在另一个Python 3.x 当我使用string时,在另一个def中find[::],string.find()表示它';s属性不';行不通,python-3.x,Python 3.x,当我在另一个def函数中使用find()类似于string.find[:]时,它会说属性错误。为什么它不起作用 def parts(phrase): space=phrase.find(' ') first_word=phrase[0:space] return first_word def rest_of_phrase(phrase): space=phrase.find(' ') rest_of_phrase=phrase[space+1:]
def
函数中使用find()
类似于string.find[:]
时,它会说属性错误。为什么它不起作用
def parts(phrase):
space=phrase.find(' ')
first_word=phrase[0:space]
return first_word
def rest_of_phrase(phrase):
space=phrase.find(' ')
rest_of_phrase=phrase[space+1:]
return rest_of_phrase
def jesus_this_part_is_hard(first_word,rest_of_phrase):
total_num=len(rest_of_phrase)
count=0
savepoint=[]
while total_num<count:
for i in total_num:
if i==' ':
savepoint+=[i]
count+=1
print(first_word," ")
x=savepoint[::-1]
for i in x:
if i==x[0]:
print(rest_of_phrase[i:]," ")
p=i
elif i!=x[0]:
print(rest_of_phrase[i:p],"")
p=i
def main():
phrase = input("Enter the phrase")
parts(phrase)
parts(rest_of_phrase)
jesus_this_part_is_hard(first_word,rest_of_phrase)
def部件(短语):
空格=短语。查找(“”)
第一个单词=短语[0:空格]
返回第一个单词
定义短语的剩余部分(短语):
空格=短语。查找(“”)
短语的剩余部分=短语[空格+1:]
返回短语的剩余部分
def jesus_这部分很难(第一个词,短语的其余部分):
总数=len(短语的剩余部分)
计数=0
保存点=[]
虽然total_num部分(短语的剩余部分)
这里短语的剩余部分是函数,因此您看到了这个错误
将主函数替换为以下内容,它将修复语法错误(不确定逻辑错误)
请复制/粘贴问题代码。我们无法将屏幕截图复制/粘贴到我们自己的系统中进行测试。请在此添加确切的错误消息抱歉新建。错误。。。您确实意识到您可以执行,first\u word,\u,rest\u of\u phrase=phrase.partition(“”)
对吗?对,但它仍然会得到错误AttributeError:“function”对象没有属性“find”谢谢!出于某种原因,它起作用了,但仍然不确定背后的逻辑。这就是答案。我注意到,对多个不同的事物使用相同的名称通常是个坏主意。在该代码中,短语的剩余部分既是函数名,也是包含该函数返回值的变量。最好使用单独的名称(不太可能引起混淆的bug)。尝试rest=rest\u of_phrase(phrase)
并将rest
作为参数传递给最后一个函数。
def main()
phrase = input("Enter the phrase")
first_word = parts(phrase)
rest_of_phrase = rest_of_phrase(phrase)
jesus_this_part_is_hard(first_word,rest_of_phrase)