0

Why is my code bringing error

import random word_list = {"Baba", "Mama", "Alfa", "Sheikh"} chosen_word = random.choice(word_list) print(chosen_word)

24th Oct 2024, 10:51 AM
Abdulrazaq Abdulmalik
Abdulrazaq Abdulmalik - avatar
4 Réponses
+ 10
Lists use square brackets []. Your word_list is a set.
24th Oct 2024, 10:59 AM
Lisa
Lisa - avatar
+ 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
24th Oct 2024, 12:14 PM
Bob_Li
Bob_Li - avatar
+ 1
Thanks
24th Oct 2024, 11:20 AM
Abdulrazaq Abdulmalik
Abdulrazaq Abdulmalik - avatar