1
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.

4
  • 3
    docs.python.org/tutorial/introduction.html Commented Feb 11, 2012 at 7:44
  • 1
    Also, look into random.choice(). This whole thing can be reduced to about 4 or 5 more Pythonic lines. Commented Feb 11, 2012 at 14:13
  • possibly one line (not regarding the import statement): for c in list(random.choice(["Candy","Monkey"])): print c :) Commented Feb 11, 2012 at 18:59
  • would I have to import random for random.choice()? Commented Feb 11, 2012 at 19:50

2 Answers 2

4

It should be pointed out that wordlist is 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 of wordlist, make sure you actually declare it as a list:

wordlist = ['Candy', 'Monkey']

I'm not sure what you're asking in your second question. Can you elaborate?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for answering my question, I found out how to do the second part myself. I should've properly learned the differences between lists and sets.
0

You are getting List is not callable because you are probably using small brackets (). If you use small brackets, and do wordlist(0), you actually make interpreter feel like wordlist is a method and 0 is it's argument.

s = worldlist[0]  # to assign "Candy" to s.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.