简体   繁体   English

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

[英]Python how to input a list of 4 words and “fill in the blanks” to make a sentence

so far I have 到目前为止,我有

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

I want to input: 我要输入:

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

so the sentence should read: Use your shiny broom and jump towards the sky! 因此该句子应为:用闪亮的扫帚跳向天空!

but it is printing: Use your adjective noun and verb towards the place! 但它正在印刷:将您的形容词名词和动词用于该位置!

any ideas what am I missing? 有什么想法我想念的吗?

Maybe something like this where you index into the input list based on the type of word you need? 也许是这样的事情,您根据需要的单词类型在输入列表中建立索引?

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]+'!')

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

You are accepting [shiny, broom, jump, sky] as the words parameter of the attack() function and immediately overriding that value by assigning ["noun", "verb", "adjective", "place"] to a variable of the same name. 您正在接受[shiny, broom, jump, sky]作为attack()函数的words参数,并通过将["noun", "verb", "adjective", "place"]分配给变量,立即覆盖该值同名。 Additionally, the elements of your [shiny, broom, jump, sky] list is missing quotation marks, unless those are meant to be variables and not strings. 此外,您的[shiny, broom, jump, sky]列表中的元素缺少引号,除非这些引号是变量而不是字符串。

Your code should read: 您的代码应为:

def attack(words):

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM