【问题标题】:Siimple Python. Not sure why my program is outputting this简单的 Python。不知道为什么我的程序输出这个
【发布时间】:2016-07-15 05:00:34
【问题描述】:

我正在编写一个程序来接收一个句子,将每个单词转换为拉丁语,然后将它作为一个句子吐出来。我不知道我在哪里搞砸了。我输入一个句子并运行它,它说

0x03547D40 处 str 对象下层的内置方法

s = input("Input an English sentence: ")
s = s[:-1]
string = s.lower

vStr = ("a","e","i","o","u")



def findFirstVowel(word):
    for index in range(len(word)):
        if word[index] in vStr:
            return index
    return -1

def translateWord():       
        if(vowel == -1) or (vowel == 0):
           end = (word + "ay")

        else:
            end = (word[vowel:] + word[:vowel]+ "ay")


def pigLatinTranslator(string):
    for word in string:
        vowel = findFirstVowel(word)
        translateWord(vowel)

    return


print (string) 

【问题讨论】:

  • 欢迎来到 SO!为了将来参考,通常会笑着包含完整的回溯,主要是因为它包含错误的行号。此外,您甚至无需发布即可发现错误!

标签: python-3.x output


【解决方案1】:

您错误地使用了较低的方法。 你应该像这样使用它string = s.lower()

括号改变了一切。当你不使用它时,Python 会返回一个对象。

内置函数应始终使用()

【讨论】:

    【解决方案2】:

    这是应该可以工作的代码的更正版本:

    s = input("Input an English sentence: \n").strip()
    string = s.lower() #lowercasing
    
    vStr = ("a","e","i","o","u")
    
    def findFirstVowel(word):
        for idx,chr in enumerate(word):
            if chr in vStr:
                return idx
        return -1
    
    def translateWord(vowel, word):       
            if(vowel == -1) or (vowel == 0):
               end = (word + "ay")
    
            else:
                end = (word[vowel:] + word[:vowel]+ "ay")
    
    def pigLatinTranslator(string):
        for word in string:
            vowel = findFirstVowel(word)
            translateWord(vowel,word)
    
        return
    
    print(string)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-16
      相关资源
      最近更新 更多