1

いくつかの単語 (コンマで区切られた) を食べて、これらの単語を含むクリーンなリストを吐き出すプログラムを作成することになっているこの問題が発生しました。問題を解決できません。何か案は?

def wordlist(word):
    return word.split(',')    

def main ():    
    sentence = input("write a few words and seperate them with , ")
    splitsentence = wordlist(sentence)
    for item in splitsentence:
        print(item)

main()
4

4 に答える 4

2

反復している特定のアイテムではなく、毎回リストを印刷しています。

の代わりにprint(splitsetnence)、必要ですprint(item)

def main():
    sentence = input("write a few words and separate them with ,")
    splitsentence = wordlist(sentence)
    for item in splitsentence:
        print (item)

また、インデントを意識してください。元の投稿のコードは正しくインデントされていないようです。

于 2013-01-27T17:16:53.310 に答える
1

input() を raw_input() に置き換えます。

于 2013-01-27T17:16:39.510 に答える
1

インデントがオフになっているraw_inputため、文字列を取得するために使用する必要があります。

def wordlist(word):
    return word.split(',')


def main():
         sentence = raw_input("write a few words and seperate them with , ")
         splitsentence = wordlist(sentence)
         for item in splitsentence:
             print(item)
main()

また、このような小さなタスクの場合、wordlist(word)関数を削除できます。

def main():
         sentence = raw_input("write a few words and seperate them with , ")
         splitsentence = wordlist.split(')
         for item in splitsentence:
             print(item)
main()
于 2013-01-27T17:18:18.003 に答える
1

raw_inputの代わりに使用し、inputに置き換えます。print(splitsentence)print(item)

インデントは、C や Java の使用{と同様に、ステートメントをグループ化する Python の方法であることに注意してください。}

あなたのコードの私のバージョンは次のとおりです。

sentence = raw_input("write a few words and seperate them with , ")
splitsentence = sentence.split(',')
for item in splitsentence:
    print item

def main()このコードは、または他の行を必要としません。

于 2013-01-27T17:48:23.020 に答える