0

How do I generate a list that will contain only the type of monster randomly chosen in choice a random number of times (1-5)?

monsters = ("trolls", "ogres", "werewolves", "dragons")
choice = random.choice(monsters)

I have tried the following which delivers the number but how do I make a list out of it?

num = random.randint(1, 5)
print("Your hero must fight against: ", num, choice)
r.abn
  • 5
  • 2

2 Answers2

0

The following should work. It might be because you did not (correctly) import random


monsters = ("trolls", "ogres", "werewolves", "dragons")

choice = random.choice(monsters)
num = random.randint(1, 5)

print("Your hero must fight against: ", num, choice)
soma
  • 91
  • 1
  • 1
  • 12
0

I am not sure whether I understand your questions, but I will answer it based on my understanding, please notify me if any mistakes.

Let's say you have a tuple of strings

monsters = ("trolls", "ogres", "werewolves", "dragons")

and you want to randomly pick random number of monsters from it, you can firstly generate a random number from the range by random.randint, and then use list generator to construct the list

nums = random.randint(1, 5) # nums = 2
monster_list = [random.choice(monsters) for i in range(nums)] 
# monster_list = ['ogres', 'trolls']
Dylan Wang
  • 86
  • 1
  • 8