import random
wordlist = {'Candy', 'Monkey'}
level = 0
while level == 0:
number = random.randint(1, 2)
if number == 1:
print 'Candy'
secword = 'Candy'
level = 2
elif number == 2:
print 'Monkey'
secword = 'Monkey'
level = 2
for i in secword:
print i
I have a couple of questions about the code I just randomly wrote (I’m a beginner)
1) How do I assign a word in a list to a variable?
ex. assign the word ‘Candy’ into a variable because I always get the error (List is not callable)
2) How do I assign the variable i (in the for loop) to a separate variable for each letter?
Thanks! Tell me if it’s not specific enough.
It should be pointed out that
wordlistis not actually a list, but a set. The difference is that a set does not allow duplicate values, whereas a list does. A list is created using hard-brackets:[], and a set is created using curly-brackets:{}.This is important because you can’t index a set. In other words, you can’t get an element using
wordlist[0]. It will give you a ‘set does not support indexing’ error. So, before you try to get an element out ofwordlist, make sure you actually declare it as a list:I’m not sure what you’re asking in your second question. Can you elaborate?