多个elif语句未运行

时间:2019-02-04 11:44:46

标签: python python-3.x

我正在做一个骰子游戏,当玩家扮演偶数时,分数增加10。但是如果数字为奇数,则分数降低5。如果用户角色加倍,则允许掷出另外一个骰子-其他陈述适用于3个骰子的总得分。我的if语句未运行。我试图将列表中的数字更改为字符串,但它不起作用。

def Player_1_Roll():
    global Player_1_Score
    Player_1_Score = 0
    Player_1_Roll_1 = random.randint(1, 6)
    print(Player_1_Name, "'s first roll is", Player_1_Roll_1)
    time.sleep(1)
    Player_1_Roll_2 = random.randint(1, 6)
    print(Player_1_Name, "'s second roll is", Player_1_Roll_2)


    Player_1_Score = Player_1_Roll_1 + Player_1_Roll_2

    if Player_1_Score == [2, 4, 6, 8, 10, 12, 14, 16, 18]:
        Player_1_Score = Player_1_Score + 10
        print(Player_1_Name, "'s Score is", Player_1_Score)

    elif Player_1_Score == [1, 3, 5, 7, 9, 11, 13, 15, 17]:
        Player_1_Score = Player_1_Score - 5
        print(Player_1_Name, "'s Score is", Player_1_Score)

    elif Player_1_Score < 0:
        Player_1_Score = 0
        print(Player_1_Name, "'s Score is", Player_1_Score)

    elif Player_1_Roll_1 == Player_1_Roll_2:
        print("")
        print(Player_1_Name, "rolled doubles!")
        print("")
        Player_1_Roll_3 = random.randint(1, 6)
        print(Player_1_Name, "'s bonus roll is", Player_1_Roll_3)
        Player_1_Score = Player_1_Score + Player_1_Roll_3 + Player_1_Roll_1 + Player_1_Roll_2
        print(Player_1_Name, "'s Score is", Player_1_Score)

1 个答案:

答案 0 :(得分:4)

似乎您正在寻找in运算符。现在,您正在尝试验证分数是否与整个列表相同。改用它:

if Player_1_Score in [2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18]:

正如评论中提到的那样,这不是很有效。而是使用元组或set。

if Player_1_Score in {2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18}:

如果您唯一需要的是分数是偶数,则可以使用模运算符。

if Player_1_Score % 2 == 0: