tags:

views:

51

answers:

6

So I'm really new to programming, I just started learning Python yesterday and I'm having a little trouble. I've looked through a few tutorials and haven't come up with how to answer my question on my own, so I'm coming to you guys.

quickList = ["string1", "string2"]
anotherList1 = ["another1a", "another1b"]
anotherList2 = ["another2a", "another2b"]

for i in range(1):
    quick=random.choice(quickList)
    another1=random.choice(anotherList1)
    another2=random.choice(anotherList2)

What I want to do is write the code so that if quick turns up string1, it will print string1 and then print another1, but if quick generates string2, it will print string2 and then an entry from anotherList2.

Any hints?

Thanks in advance for your help!

A: 

This will work for you:

quickList = ["string1", "string2"]
anotherList1 = ["another1a", "another1b"]
anotherList2 = ["another2a", "another2b"]

for i in range(1):
    if random.choice(quickList) == 'string1':
        another1=random.choice(anotherList1)
    else:
        another2=random.choice(anotherList2)
Jerub
+1  A: 

Try to think that logic through. I have formatted your exact words for you:

if (quick turns up string1):
    print string1
    print another1 //I assume you mean a string from this list
but if (quick generates string2):
    print string2 
    and then an entry from anotherList2

This is the logic you want, now you just have to translate that back to python. I will leave that to you.

In general, try to relate if statements to literal choices in logic. It will help you write code in any language.

(As an extra note, why is it in a for loop? There is no need if you only do it one time.)

Chris Cooper
A: 

if? What if?

quickList = ["string1", "string2"]
anotherList1 = ["another1a", "another1b"]
anotherList2 = ["another2a", "another2b"]

for i in range(1):
    quick = random.choice(list(enumerate(quickList)))
    anothers = [random.choice(x) for x in (anotherList1, anotherList2)]
    print quick[1]
    print anothers[quick[0]]
Ignacio Vazquez-Abrams
+2  A: 

Try storing them in a dictionary:

d = {
    'string1': ['another1a', 'another1b'],
    'string2': ['another2a', 'another2b'],
}
choice = random.choice(d.keys())
print choice, random.choice(d[choice])
tcarobruce
A: 

Your words don't match your code - does "string1" imply "another1" or does it imply a choice from anotherList1? If the latter, I'd make the association between the quicks and the anothers explicit in the data:

combinedList = [ ("string1", ["another1a", "another1b"]),
                 ("string2", ["another2a", "another2b"]) ]

quick,anotherList = random.choice( combinedList )
another = random.choice(anotherList)
Russell Borogove
A: 

Since you're new to Python, let me suggest another way of doing this.

quickList = ["string1", "string2"]
anotherList = {"string1": ["another1a", "another1b"],
               "string2": ["another2a", "another2b"]}

for i in range(1):
    quick = random.choice(quickList)
    print quick
    print random.choice(anotherList[quick])

Also as others have mentioned, not sure why your code is in a for loop. You could take that out as well, but I have left it in for this example.

This let's you expand your list more easily and saves you from building a bunch of if statements. It could be further optimized, but try and see if you understand this approach :-)

Bartek