fork download
  1. import random
  2.  
  3. actions = ["グー", "チョキ", "パー"]
  4.  
  5. def print_choices(user, pc):
  6. pcs = zip(["PC{:d}".format(i+1) for i in range(len(pc))], pc)
  7. print([("You", user)]+list(pcs))
  8.  
  9. n_pc = int(input("PCの数:"))
  10. assert n_pc >= 1
  11.  
  12. while True:
  13. pc_actions = [random.choice(actions) for _ in range(n_pc)]
  14. action = input("じゃんけんぽん!\n> ")
  15. print_choices(action, pc_actions)
  16.  
  17. act_set = set(pc_actions+[action])
  18. if len(act_set) == 2:
  19. break
  20. else:
  21. print("あいこ")
  22.  
  23. if (action == "グー" and "チョキ" in act_set) \
  24. or (action == "チョキ" and "パー" in act_set) \
  25. or (action == "パー" and "グー" in act_set):
  26. print("You win!")
  27. else:
  28. print("You lose")
Success #stdin #stdout 0.03s 36944KB
stdin
4
パー
グー
チョキ
stdout
PCの数:じゃんけんぽん!
> [('You', 'パー'), ('PC1', 'パー'), ('PC2', 'グー'), ('PC3', 'グー'), ('PC4', 'グー')]
You win!