'Having trouble in a volleyball simulation- stuck in a loop
Good afternoon, I'm trying to simulate a volleyball game based in the probability that each team has to win a point. Basically I am stuck in a loop even tho I have conditions that should make it stop. The problem is in my Set25P function. The code follows:
mostly code fillllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll
import random
def main():
Intro()
probA, probB = Data()
winsA, winsB = Sets4(probA, probB)
winsA, winsB = Set5(probA, probB, winsA, winsB)
def Intro():
print("This program simulates a volleyball match")
print("between two teams: Team A and Team B.")
print("The result of the match varies according")
print("the probability of each team.")
def Data():
probA = eval(input("Whats the probability that Team A wins the point? "))
probB = eval(input("Whats the probability that Team B wins the point? "))
return probA, probB
def Set5(probA, probB, winsA, winsB):
if winsA == winsB == 2:
scoreA, scoreB = Set15P(probA, probB)
if scoreA == scoreB + 2 and scoreA >= 15:
winsA += 1
elif scoreB == scoreA + 2 and scoreB >= 15:
winsB += 1
return winsA, winsB
def Set15P(winsA, winsB, probA, probB):
serving = "A"
scoreA = 0
scoreB = 0
while True:
if not (scoreA == scoreB + 2 and scoreA >= 15) or not (scoreB == scoreA + 2 and scoreB >= 15):
if serving == "A":
if random.random() < probA:
scoreA += 1
else:
serving = "B"
scoreB += 1
else:
if random.random() < probB:
scoreB += 1
else:
serving = "A"
scoreA += 1
elif scoreA == scoreB + 2 and scoreA >= 15:
winsA += 1
break
elif scoreB == scoreA + 2 and scoreB >= 15:
winsB += 1
break
return winsA, winsB
def Sets4(probA, probB):
winsA = 0
winsB = 0
for i in range(4):
winsA, winsB = Set25P(winsA, winsB, probA, probB)
if gameOver(winsA, winsB):
break
return winsA, winsB
def Set25P(winsA, winsB, probA, probB):
serving = "A"
scoreA = 0
scoreB = 0
while True:
if not (scoreA == scoreB + 2 and scoreA >= 25) or not (scoreB == scoreA + 2 and scoreB >= 25):
if serving == "A":
if random.random() < probA:
scoreA += 1
else:
serving = "B"
scoreB += 1
else:
if random.random() < probB:
scoreB += 1
else:
serving = "A"
scoreA += 1
elif scoreA == scoreB + 2 and scoreA >= 25:
winsA += 1
break
elif scoreB == scoreA + 2 and scoreB >= 25:
winsB += 1
break
return winsA, winsB
def gameOver(winsA, winsB):
if winsA == 3:
print("Team A wins the match")
return True
elif winsB == 3:
print("Team B wins the match")
return True
else:
return False
if __name__ == '__main__': main()
Solution 1:[1]
Expanding on my comments. Your conditions in your if and elif are wrong for what you are trying to achieve and you've wrapped yourself in big inescapable boolean straight jacket.
You want the loop to continue if neither score is greater than 25 or (if it is greater than 25) the difference in the scores is less than 2. So write that logic as-is without the not:
def Set25P(winsA, winsB, probA, probB):
serving = "A"
scoreA = 0
scoreB = 0
while True:
if (scoreA < 25 and scoreB < 25) or (abs(scoreA - scoreB) < 2):
if serving == "A":
if random.random() < probA:
scoreA += 1
else:
serving = "B"
scoreB += 1
else:
if random.random() < probB:
scoreB += 1
else:
serving = "A"
scoreA += 1
elif scoreA > scoreB:
winsA += 1
break
elif scoreB > scoreA:
winsB += 1
break
Note I also changed your elif conditions since there is no reason to do extra checking at this point. It's merely "Who won?".
As for troubleshooting, next time sprinkle some print() statements inside your loop and see what the values are of your scores. It will help illuminate the issue.
Solution 2:[2]
Tried running the code, and found that the problem lies with the line
if(scoreA == scoreB + 2 and scoreA >= 25)
You probably want scoreA >= scoreB + 2 and scoreA >= 25. The problem currently is that scoreA can only win if it's scoreB+2 AND larger than 25. Meaning that a score of 27-23 doesn't mean that scoreA wins because scoreB + 2 = 25, which is not scoreA.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jhanzaib Humayun |
