Вы должны поместить random.sample()
в l oop.
содержимое test.txt:
this
is
my
test
file
for
so
Код:
import random
howmanytimes = int(input("How many times?:"))
people = int(input("How many people do you want to comment?:"))
samples = open("test.txt", "r").readlines()
for i in range(howmanytimes):
mentions = random.sample(samples, people)
print(mentions)
Вывод:
>>> python3 test.py
How many times?:3
How many people do you want to comment?:2
['this\n', 'is\n']
['this\n', 'file\n']
['test\n', 'so']
Примечание:
Вы можете удалить завершающие символы новой строки (\n
) из строк со следующей строкой:
samples = list(map(str.strip, open("test.txt", "r").readlines()))
В этом случае вывод будет:
>>> python3 test.py
How many times?:3
How many people do you want to comment?:2
['test', 'my']
['so', 'test']
['this', 'test']