I'm very new to Python and I'm having an issue setting a new variable after using the random command.
Here is my sample code:
import random
foo = ['a', 'b']
print("foo is: " + random.choice(foo))
if foo == 'a':
randomLetter = 'a'
else:
randomLetter = 'b'
print(randomLetter)
No matter what foo equals, randomLetter always equals b.
I'm not sure what I am doing wrong. Any help with be appreciated.
foo. If you printfooyou will see that it is['a', 'b']. You maybe want to makefoo = random.choice(foo)foo = ['a', 'b'], you're saying thatfoois a list of two items. So when you get toif foo == 'a', that's the same as checking if the list['a', 'b']is the same thing as the string'a'. Obviously this will never be the case; one is a list and the other is a string. The commandprint("foo is: " + random.choice(foo))randomly picks an item from your list and prints it, but it doesn't make any changes to the list itself.random.choice(foo)just picks a random item from that list to print it.