有没有一种方法可以检查某个物体是否存在另一个物体?

时间:2017-11-20 02:20:21

标签: python

我想测试我的一个集合中的第一个数字是否与两个不同集合中的任何一个中的数字相同,然后对第二个,第三个等同样做。如果是第一个数字是与玩家设置的第一个号码相同,我想让玩家能够做一个动作,但如果它是敌人的设置,那么我希望他们去。有没有办法实现这个目标?

Pm1 = input("What is your knight's name?")
Pm2 = input("What is your theif's name?")
Pm3 = input("What is your doctor's name?")
Pm4 = input("What is your priest's name?")
import random
Pm1Order = random.randint(1,8) 
Pm2Order = random.randint(1,8)
if Pm2Order == Pm1Order: 
    Pm2Order = Pm1Order - 1
    if Pm2Order == 0:
        Pm2Order = Pm1Order + 1
Pm3Order = random.randint(1,8) 
if Pm3Order == Pm2Order:
    Pm3Order = Pm1Order - 2
    if Pm3Order == 0:
        Pm3Order = Pm1Order + 2
Pm4Order = random.randint(1,8)
if Pm4Order == Pm3Order:
    Pm4Order = Pm1Order - 3
    if Pm4Order == 0:
        Pm4Order = Pm1Order + 3
print("The turn orders for your party is " +str(Pm1Order)+ " for the knight, " +str(Pm2Order)+ " for the theif, " +str(Pm3Order)+ " for the doctor, and " +str(Pm4Order)+ " for the priest.")
PlayerOrder = set([Pm1Order , Pm2Order , Pm3Order , Pm4Order])
print(str(PlayerOrder))
FullOrder = set([1, 2, 3, 4, 5, 6, 7, 8])
EnemyOrder = FullOrder.difference(PlayerOrder)
EnemyOrder2 = FullOrder.difference(PlayerOrder)
print(str(EnemyOrder))
Enemy1Order = random.sample(EnemyOrder, 1)
Enemy2Order = random.sample(EnemyOrder, 1)
Enemy3Order = random.sample(EnemyOrder, 1)
Enemy4Order = random.sample(EnemyOrder, 1)
print(Enemy1Order)
AllOrder = set([Pm1Order, Pm2Order, Pm3Order, Pm4Order, Enemy1Order, Enemy2Order, Enemy3Order, Enemy4Order])
AllOrder.sort(key = int)
print(AllOrder)
####Test for first element if its player then they go if its enemey they go and 

1 个答案:

答案 0 :(得分:0)

使用min代替set.sort。由于set没有任何内部排序,因此您无法对其进行排序。你可以使用sorted(your_set),但如果你关心的只是最小的元素,那就是你需要的全部内容。

first_turn = min(AllOrder)

if first_turn in EnemyOrder:
    ...
elif first_turn in PlayerOrder:
    ...