【问题标题】:Python3 Cannot concatenate error? How to fix?Python3无法连接错误?怎么修?
【发布时间】:2017-08-20 21:51:14
【问题描述】:

鉴于我构建的这个函数,我收到最后一个返回语句的错误,说我不能将它们连接在一起。有没有其他方法可以做到这一点?

def simple_pig_latin(input, sep=' ', end='.'):
words=input.split(sep)
Vowels= ('a','e','i','o','u')
Digit= (0,1,2,3,4,5,6,7,8,9)
cons=('b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z')
characters= ('!','@','#','$','%','^','&','*','.')
new_sentence=[]

for word in words:
    if word:
        if word[0]==" ":
            new_word=word   
        else:
            if word[0] in Vowels:
                new_word= word+"way"

            elif word[0] in Digit:
                new_word= word

            elif word[0] in cons:
                first_letter=word[0] #saves the first letter
                change= str(word) #change to string                     
                rem= change.replace(first_letter,'')#remove the first letter
                put_last= rem+first_letter #add letter to end
                new_word= put_last+ "ay"

            elif word[0] in characters:
                new_word= word

            new_sentence= new_sentence + new_word+ sep
    else:
        new_sentence.append(word)

return sep.join(new_sentence)+end

【问题讨论】:

    标签: function python-3.x join concatenation


    【解决方案1】:

    我认为这就是您要寻找的。 (你需要使用 ''.join() )

    def simple_pig_latin(input, sep=' ', end='.'):
        words=input.split(sep)
        Vowels= ('a','e','i','o','u')
        Digit= (0,1,2,3,4,5,6,7,8,9)
        cons=('b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z')
        characters= ('!','@','#','$','%','^','&','*','.')
        new_sentence=[]
        for word in words:
            if word:
                if word[0]==" ":
                    new_word=word
                else:
                    if word[0] in Vowels:
                        new_word= word+"way"
                    elif word[0] in Digit:
                        new_word= word
                    elif word[0] in cons:
                        first_letter=word[0] #saves the first letter
                        change= str(word) #change to string
                        rem= change.replace(first_letter,'')#remove the first letter
                        put_last= rem+first_letter #add letter to end
                        new_word= put_last+ "ay"
                    elif word[0] in characters:
                        new_word= word
                    new_sentence= ''.join(new_sentence) + ''.join(new_word) + ''.join(sep)
            else:
                new_sentence.append(word)
    
        "".join(new_sentence)
        return new_sentence
    
    
    
    
        print simple_pig_latin("welcome to the jungle.")  # Sample call to func
    

    结果

        lcomeway otay hetay ungle.jay
    

    编辑 1

    另一种方式

    def simple_pig_latin(input, sep=' ', end='.'):
        words = input.split(sep)
        sentence =""
        for word in words:
            endString= str(word[1:])
            them=endString, str(word[0:1]), 'ay'
            newWords="".join(them)
            sentence = sentence + sep + newWords
        sentence = sentence + end
        return sentence
    
    
        print simple_pig_latin("I like this ")
    

    【讨论】:

    • 我不能使用打印语句,只能返回@Cyrus
    • 抱歉,print 是用来调试的。删除它,请尝试。它可以工作(只要你期望的输出是我得到的(欢迎来到丛林。返回为 elcomeway otay hetay ungle.jay
    • jay前面不能有圆点
    • 请分享示例输入和预期输出。我怀疑问题出在上面的编码上,而不是你之前遇到的关于加入的错误。
    • 我可以建议你试试我的“替代方式”吗?更短的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    相关资源
    最近更新 更多