Python如何输入4个单词的列表并“填空”来制作句子

时间:2017-10-25 03:46:29

标签: python

到目前为止我已经

def attack(words):
     words = ['noun', 'verb', 'adjective', 'place']
     print('Use your', words[2], words[0], 'and', words[1], 'towards the', words[3]+'!')

我想输入:

attack([shiny, broom, jump, sky])   

所以句子应该是这样的:用闪亮的扫帚跳到天空!

但它正在打印:使用你的形容词名词和动词来到这个地方!

任何想法我错过了什么?

2 个答案:

答案 0 :(得分:4)

也许是这样的,根据你需要的单词类型将输入列表编入索引?

def attack(words):
     noun, verb, adjective, place = 0, 1, 2, 3
     print('Use your', words[adjective], words[noun], 'and', words[verb], 'towards the', words[place]+'!')

答案 1 :(得分:2)

删除words = ["noun", "verb", "adjective", "place"]

您接受[shiny, broom, jump, sky]作为words函数的attack()参数,并通过将["noun", "verb", "adjective", "place"]分配给同名变量来立即覆盖该值。此外,[shiny, broom, jump, sky]列表的元素缺少引号,除非它们是变量而不是字符串。

您的代码应为:

def attack(words):

    print("Use your", words[2], words[0], "and", words[1], "towards the", words[3] + "!")

attack(["shiny", "broom", "jump", "sky"])