-1

I am making a dice game, when the player roles an even number, the score is increased by 10. However if the number is odd your score is decreased by 5. If the user roles doubles the are allowed to roll an additional dice - the other statements apply to the total score of 3 dice. My if statements are not running. I have tried to change the numbers in the list to strings, it does not work.

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)
Partho63
  • 3,117
  • 2
  • 21
  • 39
Kian L
  • 73
  • 8

1 Answers1

4

It seems that you are looking for the in operator. Now you are trying to verify whether the score is identical to that whole list. Use this instead:

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

As mentioned in the comments, this is not efficient though. Instead use a tuple or set instead.

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

If the only requirement that you have, is that the score is even, you can use the modulus operator.

if Player_1_Score % 2 == 0:
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
  • 1
    You should really use a set or tuple. The CPython compiler is at least smart enough to replace the list here with a tuple so it can be stored as a constant, but set membership testing is faster. Another option is to use `range(2, 20, 2)`, or to use the modulus operator to see if the score is even. – Martijn Pieters Feb 04 '19 at 11:52
  • @MartijnPieters Good call. I'm not sure whether range is faster though. Do you have any reference for that? I can imagine that it is a generator below the hood, which - if I recall correctly - is a bit slower when checking membership. – Bram Vanroy Feb 04 '19 at 12:01