0

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)

2 Answers 2

1

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

  1. Use randint() function of the random module to randomly select an a number between 0 and length-1 of your list
  2. Use this number as index to replace a random element in your list with a new boolean value

Update

You 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

  1. Initialize your elements as a dict with values as boolean, rather than list
  2. Use random.choice() function of the random module to randomly select an a key from dict
  3. Use this key to replace a random boolean in your dict with a new boolean value
Sign up to request clarification or add additional context in comments.

2 Comments

I tried to reproduce your explanation but it seems that I do not know how to apply it to my code. I don't understand how to call / use the character which has become True
@PabloS From whatI understand from the update you want to associate this boolean value with a person, you should use a python dict for that, I'll update the answer
0

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!

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.