Aystealthy
Aystealthy

Reputation: 169

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?

Upvotes: 1

Views: 1426

Answers (2)

Reblochon Masque
Reblochon Masque

Reputation: 36662

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

Upvotes: 4

Erick Shepherd
Erick Shepherd

Reputation: 1443

Remove 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. Additionally, the elements of your [shiny, broom, jump, sky] list is missing quotation marks, unless those are meant to be variables and not strings.

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"])  

Upvotes: 2

Related Questions