A Python Model for Ping Pong Matches

See Python: Tips and Tricks for similar articles.
Looking to improve your Python skills? Check out Webucator’s Python classes.

A while back, I started playing table tennis more seriously. I’m now in sort of a no-man’s land, in which I can beat most people who consider it a basement game, but can’t beat very many people who consider it a sport.

Because I’d rather write Python code than practice against my robot, I’ve created a model in Python to try to get an idea of what I should practice most to win more games. I’m unlikely to follow through on the practice, but I am likely to refine the model if people have ideas of how it could be made better. So, if there are any ping pong programmers out there, please feel free to make suggestions.

Before we get into the Python code, let me explain a little about table tennis.

Relevant Table Tennis Rules:

  1. Matches are best of 5 games.
  2. Games are played to 11 points, but you must win by 2.
  3. The server changes every two points up until deuce deuce (11-11), at which point it changes every point.
  4. The first to serve in the first game is chosen randomly and then alternated each subsequent game.
  5. In table tennis, you only get one fault.
If your serve doesn’t go in, you lose the point.

How Points Play Out

The server tries to make it difficult for his opponent to return the serve well. Ideally, the opponent will not be able to return the serve at all, but if he does, the server hopes the return is weak enough so that he can easily put the third ball away. You can think of a point like this:

  1. Server serves
  2. Returner returns serve
  3. Server attempts putaway
  4. Returner volleys
  5. Server volleys
  6. Returner volleys

Once the volley has lasted for five hits, the odds of a player successfully returning a shot probably don’t change much with each successive hit.

So, when two players face each other, you can create a model to show how the matches are likely to come out based on the players’ likelihood to fail on any given shot. For example, imagine these two players failure rates when they play each other:

LeiraNat
Serve.05.1
Return of Serve.1.5
3rd Shot.3.25
4th Shot.2.4
5th Shot.2.25
6th Shot.2.25
  • When Leira serves first, he almost always gets it in.
  • Nat has a lot of trouble with Leira’s serve. He hits half of them into the net or off the table.
  • When Nat does get it back, he sets Leira up for a putaway. And Leira generally goes for it, but he hits it out 30% of the time.
  • As per above, Leira’s 3rd shot is often, but not always, an attempted putaway. Nat fails to return that shot about 40% of the time.
  • If it gets to a fifth shot, the point becomes more of a volley and the chances of a putaway or an error on any given shot are lower. Leira only fails to return the fifth and subsequent shots 20% of the time.
  • At this point in the volley, Nat fails to return shots 25% of the time.
  • When Nat serves first, he gets 90% of his serves in.
  • Leira has little trouble returning Nat’s serve. He gets 90% of them back.
  • Nat doesn’t often set himself up for the 3rd shot kill, so he moves right to volley mode, in which he fails to return shots 25% of the time.
  • Leira is in volley mode at this point too and fails to return shots 20% of the time.

So, let’s consider our players in Python:

#Leira
player.name = 'Leira'
player.fail_rates = [.05,.1,.3,.2]
#Nat
player.name = 'Nat'
player.fail_rates = [.1,.5,.25,.4,.25]

My program assumes that the fail rate for all hits after the last one indicated in the player’s fail_rates list is the same as the fail rate for the last one listed. So, because Leira has the same fail rate for shots 4, 5, and 6 and all subsequent shots, we only have to indicate the fail rate for his first four shots.

So, those are my assumptions. You can check out the Python program here.

Feel free to play around with it and let me know if you have any suggestions for improving the model or the code.

Written by Nat Dunn.


Related Articles

  1. Scientific Notation in Python
  2. Understanding Python’s __main__ variable
  3. Associate Python Files with IDLE
  4. Python: isdigit() vs. isdecimal()
  5. Python Color Constants Module
  6. Python: pow(x, y, z) less efficient than x**y % z
  7. A Python Model for Ping Pong Matches (this article)
  8. Bulk Convert Python files to IPython Notebook Files (py to ipynb conversion)
  9. Collatz Conjecture in Python
  10. Finally, a use case for finally – Python Exception Handling
  11. Python Clocks Explained
  12. Python’s date.strftime() slower than str(), split, unpack, and concatenate?
  13. Bi-directional Dictionary in Python
  14. Maximum recursion depth exceeded while calling a Python object
  15. Basic Python Programming Exercise: A Penny Doubled Every Day
  16. Creating an Email Decorator with Python and AWS
  17. How to Create a Simple Simulation in Python – Numeric Data
  18. Python Coding Challenge: Two People with the Same Birthday
  19. How to find all your Python installations on Windows
  20. Change Default autosave Interval in JupyterLab
  21. Interactive Quiz using IPython Notebook
  22. When to use Static Methods in Python? Never
  23. Converting Leading Tabs to Spaces with Python
  24. Simple Python Script for Extracting Text from an SRT File
  25. Python Virtual Environments with venv
  26. Mapping python to Python 3 on Your Mac
  27. How to Make IDLE the Default Editor for Python Files on Windows
  28. How to Do Ternary Operator Assignment in Python
  29. How to Convert Seconds to Years with Python
  30. How to Create a Python Package
  31. How to Read a File with Python
  32. How to Check the Operating System with Python
  33. How to Use enumerate() to Print a Numbered List in Python
  34. How to Repeatedly Append to a String in Python
  35. Checking your Sitemap for Broken Links with Python
  36. How to do Simultaneous Assignment in Python
  37. Visual Studio Code - Opening Files with Python open()
  38. How to Slice Strings in Python
  39. How Python Finds Imported Modules
  40. How to Merge Dictionaries in Python
  41. How to Index Strings in Python
  42. How to Create a Tuple in Python