Why do I get a None output when I use this if statement

Viewed 28
 Alph= ["a","A","b","B","c","C","D","d","e","E","f","F","g","G","h","H","i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X","y","Y","z","Z"]
Num = [1,2,3,4,5,6,7,8,9,0]

  D1 = random.randint(1,6)
  D2 = random.randint(1,6)   
  P1_S = 0  
        P1_MS = input("Type a letter to begin player 1 and roll dice")
    if P1_MS == Alph or Num:
                 P1_S = P1_S + D1 + D2, print("First roll is", D1), print("Second roll is", D2)
                 print(P1_S, "is the total amount")

After this I appear to get the "none, none" part so how can I remove that from my output? Output:

First roll is 3
Second roll is 3
(6, None, None) is the total amount

How can I remove the None, None at the end of the output?

3 Answers

It's because of this line:

P1_S =P1_S+D1+D2 , print("First roll is", D1) , print("Second roll is", D2)

The commas mean you're assigning P1_S to be a three-element tuple. The first element is P1_S+D1+D2, the second element is the result of a print() function (which always returns None), and the third element is another print.

So, you end up with (6, None, None) as the value.

You haven't mentioned what is P1_S in your code, or where you have declared it. I guess you have made it a list or some other datatype than simple Int. Check it.

Please give more code (in the snippet P1_S is not defined)

If you want to learn: Try it with a new .py file, only for the stack overflow question.

If you only want an answer, try this, and give feedback:

P1_S =int(P1_S)+int(D1)+int(D2)
print("First roll is", int(D1))
print("Second roll is", int(D2))
print (str(P1_S[0]),"is the total amount")

The important part for you is in the last line P1_S[0] access the first entry ("6") which should be your result as I understand.

Related