0
Why is my code bringing error
import random word_list = {"Baba", "Mama", "Alfa", "Sheikh"} chosen_word = random.choice(word_list) print(chosen_word)
4 Réponses
+ 10
Lists use square brackets [].
Your word_list is a set.
+ 6
or use parenthesis().
tuples are also valid input for random .choice
list and tuples can be accessed by index. sets cannot be accessed by index.
random.choice() returns a randomly selected element from the specified sequence.
The sequence can be a string, a range, a list, a tuple or any other kind of sequence.
a set is not a sequence since it is not ordered, and its values can't be accessed by keys or indexes
+ 1
Thanks