Reputation: 27
I was wondering if it was possible to create a code like the one quoted.
I mean if it is possible to create an array with booleans (False), choose only one (randomly) and change its value (to True) and then be able to use it?
The first random choice should only be done once, when the program is launched.
Gentleman = False
Daughter = False
Madam = False
character_list = [Gentleman, Daughter, Madam] # an array with the bools above
for character in characters_list:
character = random.choice(characters_list) # Randomly pick a character from the list
character = True # Set the character as True
if Madam == True:
...
Madam = False
Daughter = True
...
if Gentleman == True:
...
Gentleman = False
Madam = True
...
if Daughter == True:
...
Daughter = False
Gentleman = True
...
PS: I know my code is wrong but it's just to try to illustrate what I would like to do
Thank you for your help
Update
So after trying what is written in the comment, I find myself with a problem
Gentleman = False
Daughter = False
Madam = False
character_list = [Gentleman, Daughter, Madam] # an array with the bools above
print(characters_list)
characters_list[random.randint(0, len(characters_list)-1)] = True
print(characters_list)
print("Starting up ...")
if Madam == True:
print("Hello Madam")
...
Madam = False
Daughter = True
...
Output
[False, False, False]
[False, False, True]
Starting up ...
Since I don't go into the if Madam == True:
I guess I don't quite understand how to use it, what should I do to get my code to understand that the Madam has turned True and goes into the if statement
(Btw I am new to Python)
Upvotes: 0
Views: 780
Reputation: 2129
To create an array with booleans (False) and randomly change one of it's value (to True) try:
Code:
import random
my_list = [False]*5
print(my_list)
my_list[random.randint(0,len(my_list)-1)] = True
print(my_list)
Input
[False, False, False, False, False]
Output
[False, True, False, False, False]
Explanation
randint()
function of the random
module to randomly select an a number between 0 and length-1
of your listYou code seem to warrant a python dict
so try modifying the above logic to:
Code:
import random
my_dict = {'Gentleman': False, 'Daughter': False, 'Madam': False}
print(my_dict)
my_dict[random.choice(list(my_dict.keys()))] = True
print(my_dict)
if my_dict['Madam'] == True:
print("Hello Madam")
my_dict['Madam'] = False
my_dict['Daughter'] = True
print(my_dict)
Input:
{'Gentleman': False, 'Daughter': False, 'Madam': False}
Output:
# initial values
{'Gentleman': False, 'Daughter': False, 'Madam': False}
# after random value is set to True
{'Gentleman': False, 'Daughter': False, 'Madam': True}
#inside the if condition
Hello Madam
{'Gentleman': False, 'Daughter': True, 'Madam': False}
Explanation
dict
with values as boolean, rather than listrandom.choice()
function of the random
module to randomly select an a key from dict
dict
with a new boolean valueUpvotes: 1
Reputation:
It's not too complicated. Essentially, what you want to do is pick a random index in the the list, and change that item. For example,
import random
bool_list = [False, False, False]
list_length = len(bool_list)
index = random.randint(list_length)
bool_list[index] = True
or, wrapped into a function:
def set_random_true(list_):
list_len = len(list_)
index = random.randint(index)
list_[index] = True
Then, you can use it like this:
import random
def set_random_true(list_):
--snip--
my_list = [False, False, False]
set_random_true(my_list)
print(my_list)
This would either print [True, False, False]
, [False, True, False]
, or [False, False, True]
. Hope this helped!
Upvotes: 0