2

I am starting out with python and tried to make a heads or tails game with little understanding so i could get used to it. I am trying find out a way to output one value (heads) if the other has already been used (tails).

Here is part of my code, i need help with P2:

P1 = str(input("P1, please enter Heads or Tails "))
if P1 in ['Heads','heads','HEADS']:
    print("You are Heads")
elif P1 in ['Tails','TAILS','tails']:
     print("You are Tails")
else:
    print("Oops, Thats not right")
    exit()
time.sleep(1)
P2 = str(input("P2, please enter heads or tails"))





time.sleep(1)
print("The coin has been flipped")
time.sleep(1)
HoT = random.randint(1,2)
if HoT == 1:
    print ("heads wins")
else:
    print("tails wins")
time.sleep(1)
exit()
James Carr
  • 31
  • 1
  • 3
    What do you mean by "already used"? Can you give an example of what the user(s) might type, what should happen as a result, and how that is different from what currently happens? It sounds like you want player 2 to use whichever option wasn't chosen by player 1, but now I'm confused. There's only one option remaining, so why are you asking player 2 a question at all? – Karl Knechtel Nov 10 '20 at 23:03
  • Welcome to Stack Overflow! If you still have questions, ask them in the comments. If an answer solved your problem, accept and/or upvote it :) – Ann Zen Nov 11 '20 at 03:51

3 Answers3

1

If you are trying to display which (heads or tails) player 2 will be you can use an if statement, as you have done at the start of your code:

# after P1 has chosen...
if P1 in ['Heads','heads','HEADS']:
    P2 = "tails"
else:
    P2 = "heads"

print("P2 you are {}".format(P2))

You can also use the ternary operator

# after P1 has chosen...
P2 = "tails" if P1 in ['Heads','heads','HEADS'] else "heads"
print("P2 you are {}".format(P2))
Tony
  • 8,904
  • 3
  • 41
  • 67
  • okay thank you, i realised that i dont need to ask player 2 a question because there is only one option left and i could just tell them which it is. but thank you anyway :) – James Carr Nov 27 '20 at 22:17
0

If understand you correctly, here is what you could try.

out = 'Heads' if P1.lower() == 'tails' else 'Tails'

print(out)
0

Here is how:

import random, time

P1 = input("P1, please enter Heads or Tails ")
if P1.lower() == 'heads':
    print("You are Heads")
elif P1.lower() == 'tails':
     print("You are Tails")
else:
    print("Oops, Thats not right")
    exit()
time.sleep(1)

P2 = 'heads' if P1.lower() == 'tails' else 'tails' # Here is the condiiton

time.sleep(1)
print("The coin has been flipped")
time.sleep(1)
HoT = random.randint(1,2)
if HoT == 1:
    print ("heads wins")
else:
    print("tails wins")
time.sleep(1)
exit()
Ann Zen
  • 17,892
  • 6
  • 20
  • 39