【发布时间】:2021-08-19 17:57:51
【问题描述】:
我想知道是否可以创建与引用的代码类似的代码。
我的意思是如果可以用布尔值 (False) 创建一个数组,只选择一个 (随机) 并更改它的值 (为 True) 然后就可以使用它了吗?
第一个随机选择只能在程序启动时进行一次。
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:我知道我的代码是错误的,但这只是为了说明我想做什么
感谢您的帮助
更新
所以在尝试了评论中写的内容后,我发现自己遇到了问题
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
...
输出
[False, False, False]
[False, False, True]
Starting up ...
由于我没有进入if Madam == True:,我想我不太明白如何使用它,我应该怎么做才能让我的代码明白女士已经变成了True并进入了if语句
(顺便说一句,我是 Python 新手)
【问题讨论】:
标签: python arrays random boolean