/python/scorekeeper.py

https://github.com/miller-time/scorekeeper · Python · 78 lines · 62 code · 5 blank · 11 comment · 5 complexity · 627d9380fc66cabb7d5e5e44abf4ea20 MD5 · raw file

  1. import sys, os
  2. """
  3. The original version of Scorekeeper
  4. Just a text-only and extremely simple program that
  5. I wrote in about an hour.
  6. """
  7. players = {}
  8. def init():
  9. """
  10. initialize the players dict by asking for a series of names
  11. """
  12. while True:
  13. name = raw_input("Please enter player name[blank if done]: ")
  14. if not name:
  15. break
  16. players[name] = 0
  17. if not players:
  18. sys.exit(0)
  19. def printscore():
  20. """
  21. I'm amazed that this function works...
  22. Sort of iterate through the dict, printing the scores...
  23. """
  24. print("Current Score:")
  25. print("\t".join(players.keys()))
  26. scores = ""
  27. for value in players.values():
  28. scores += str(value)+"\t"
  29. print(scores)
  30. def getscore():
  31. """
  32. Ask for an update for each player's score.
  33. """
  34. for player in players:
  35. while True:
  36. try:
  37. val = int(raw_input("Score for "+player+"['-1' to exit]: "))
  38. if val == -1:
  39. end_game()
  40. break
  41. except ValueError:
  42. print("Oops, invalid input!")
  43. players[player] += val
  44. def main():
  45. """
  46. loop back and forth between showing the scores and updating them
  47. """
  48. init()
  49. while True:
  50. printscore()
  51. getscore()
  52. def snd(x):
  53. return x[-1]
  54. def end_game():
  55. """
  56. just announce the winner, using deprecated os.system! eww!
  57. """
  58. (winner, score) = sorted(players.items(), key=snd, reverse=True)[0]
  59. os.system("figlet the winner is "+winner+"!")
  60. sys.exit(0)
  61. if __name__=="__main__":
  62. main()