import random
total_data = [i for i in range(1,15)]
forbidden = [12, 13, 8, 7]
not_forbidden =[i for i in total_data if i not in forbidden] #make a list of not forbidden numbers
a = random.choice(not_forbidden) #select value which is between 7 and 14 and not in forbidden list
print(a)
也可以编写自己的自定义函数:
import random
Forbidden = [12, 13, 8, 7]
def get_number(forbidden_list):
while True:
a = random.randint(7,14)
if a not in forbidden_list: #if this number if not forbidden list then break the loop and return the value
return a
result = get_number(Forbidden)
print(result)
您可以使用
choice
函数:也可以编写自己的自定义函数:
你可以试试
random.choice()
:你知道一个真正的纸牌游戏如何阻止你抽已经抽过的牌吗?不涉及抽牌黑名单。有一副牌,你洗牌。你知道吗
在你的程序中做同样的事情。与其试图维持抽牌的黑名单,不如在程序开始时洗牌一副牌,然后从牌堆中抽牌:
相关问题 更多 >
编程相关推荐