3

i'm pretty new to python and i'm having trouble with passing an argument into the random.choice function.

basically i'm trying to write a loop that will start off by selecting a random letter(which are all names of other lists) from the 'a' list, then input the selected letter into random.choice again to generate a sequence of random letters.

unfortunately the second call to random.choice isn't working, it just keeps repeating the inputted letter instead of pointing to a new list. any ideas how to fix this? i've been looking online for a few hours but can't find any similar problems/solutions. any help would be much appreciated!

import random

a = ['b','c','d']
b = ['a','e']
c = ['a','d','f']
d = ['a','c','e','f','g','h']
e = ['b','d','h']
f = ['c','d','g','i']
g = ['d','f','i']
h = ['d','e','g','i']
i = ['f','h']

x = 0
answer = random.choice(f)
print "answer is %s " % answer

while x < 5:
    answer2 = random.choice(answer) 
    print "answer2 is now %s " % answer2
    x = x + 1

3 Answers 3

4

You've fallen into the common beginner's trap of mixing up variables and data, and trying to treat your code as data. You shouldn't typically try to do that - don't put the name of a variable into another variable and try to access it that way.

Instead, keep all the data together in a data structure, and just use your variables to access that structure, like this:

import random

DATA = {
    'a': ['b','c','d'],
    'b': ['a','e'],
    'c': ['a','d','f'],
    'd': ['a','c','e','f','g','h'],
    'e': ['b','d','h'],
    'f': ['c','d','g','i'],
    'g': ['d','f','i'],
    'h': ['d','e','g','i'],
    'i': ['f','h'],
}

x = 0
answer = random.choice(DATA['f'])
print "answer is %s " % answer

while x < 5:
    answer2 = random.choice(DATA[answer]) 
    print "answer2 is now %s " % answer2
    x = x + 1
Sign up to request clarification or add additional context in comments.

3 Comments

What difference between variables and data? A variable name is just an entry in a dictionary, hidden behind some syntactic sugar. Considered downvoting, but your method is still the correct one, even if you're oversimplifying.
Keeping the two separate makes the code easier to understand, and easier to change without unintended consequences. (All programming is just machine code, hidden behind some syntactic sugar. You wouldn't suggest that @declanb codes directly in machine code. :-) )
No. But there are situations where it's good to know how to use getattr, and the idea that there is no real difference between a dictionary key and an object attribute name is important to understanding how Python works.
0

Well... one issue is that you're always getting answer2 from the same list, since you're not doing any other assignments...

Comments

-1

Just suppose x == 'c'. Then the second call to random.choice is actually

answer2 = random.choice('c')

This is like selecting a letter from a list containing only 'c'. This is NOT equivalent to the following:

answer2 = random.choice(c)

You need to convert the string 'c' into a list referenced by the variable c.

This mapping may work:

values = {
    "a": a, "b": b, "c": c, "d": d, "e": e,
    "f": f, "g": g, "h", h, "i": i, "j": j,
    }

Then values['c'] is the list referenced by variable c

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.