Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9265929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:11:54+00:00 2026-06-18T14:11:54+00:00

I edited my previous question because I came up with the code I think

  • 0

I edited my previous question because I came up with the code I think is correct.
The logic behind this should be:
while the set is not over and it’s not a tie 10:10: player A starts serving and does it twice regardless he wins points or not, then player B takes serve and does it twice also. It continues until the set is over, except there is a tie 10:10 when servers change each point scored.

Can anyone check if the code is flawless? thank you.

def simOneSet(probA, probB):
    serving = "A"
    scoreA = scoreB = 0
    while not setOver(scoreA, scoreB):
        if scoreA != 10 and scoreB != 10:
            if serving == "A":
                for i in range(2):
                    if random() < probA:
                        scoreA += 1
                    else:
                        scoreB += 1
                serving = "B"
            else:
                for i in range(2):
                    if random() < probB:
                        scoreB +=1
                    else:
                        scoreA += 1
                serving = "A"    
        # when there is a tie 10:10
        else:
            if serving == "A":
                if random() < probA:
                    scoreA += 1
                    serving = "B"
                else:
                    scoreB += 1
                    serving = "B"
            else:
                if random() < probB:
                    scoreB += 1
                    serving = "B"
                else:
                    scoreA += 1
                    serving = "A"
    return scoreA, scoreB
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:11:55+00:00Added an answer on June 18, 2026 at 2:11 pm

    I would use a dict to “switch” between players:

    other = {'A':'B', 'B':'A'}
    

    Then, if serving equals 'A', then other[serving] would equal 'B', and if serving equals 'B', then other[serving] would equal 'A'.


    You could also use a collections.Counter to keep track of the score:

    In [1]: import collections
    
    In [2]: score = collections.Counter()
    
    In [3]: score['A'] += 1
    
    In [4]: score['A'] += 1
    
    In [5]: score['B'] += 1
    
    In [6]: score
    Out[6]: Counter({'A': 2, 'B': 1})
    

    Also notice how in this piece of code

        if serving == "A":
            for i in range(2):
                if random() < probA:
                    scoreA += 1
                else:
                    scoreB += 1
        else:
            for i in range(2):
                if random() < probB:
                    scoreB +=1
                else:
                    scoreA += 1
    

    there are two blocks which are basically the same idea repeated twice. That’s a sign that the code can be tightened-up by using a function. For example, we could define a function serve which when given a probability prob and a player (A or B) returns the player who wins:

    def serve(prob, player):
        if random.random() < prob:
            return player
        else:
            return other[player]
    

    then the above code would become

        for i in range(2):
            winner = serve(prob[serving], serving)
            score[winner] += 1
    

    Thus, you can compactify your code quite a bit this way:

    import random
    import collections
    other = {'A':'B', 'B':'A'}
    
    def serve(prob, player):
        if random.random() < prob:
            return player
        else:
            return other[player]
    
    def simOneSet(probA, probB):
        prob = {'A':probA, 'B':probB}
        score = collections.Counter()
    
        serving = "A"
        while not setOver(score['A'], score['B']):
            for i in range(2):
                winner = serve(prob[serving], serving)
                score[winner] += 1
            if score['A'] == 10 and score['B'] == 10:
                winner = serve(prob[serving], serving)
                score[winner] += 1
                serving = winner
    
        return score['A'], score['B']  
    
    def setOver(scoreA, scoreB):
        return max(scoreA, scoreB) >= 21
    
    print(simOneSet(0.5,0.5))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
My previous question got closed as not constructive. I edited it there, but didn't
Edited: Thanks for the previous answers and help! I've decided to edit this question
As per my previous question (many thanks to Jonathan Leffler), I edited my code
This is my edited previous question for line breaks before the preserve space elements.
Edited: SOLUTION FOUND. This is strange and not the best solution, but I just
So, this is something of a followup to my previous question. I'm running into
This is related to a previous question I asked with Jquery. If I have
This is a pretty small question that has been almost resolved in a previous
Repeat of a previous post, this time with the original code. I'm creating a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.