The below Python code has an odd behavior, which I can't figure out.
The program calls testQuery, which asks the used to reply 'yes' to call scoreAverager, or 'no' to exit the program. If scoreAverager is called, it then requests the user enter a series of scores, or 'x' to finish, which returns the user to testQuery, where they can chose to average another test or exit.
The weirdness happens if a user averages the results for several tests (e.g. 2 or more). At that point giving 'no' to testQuery will fail to terminated the program. It will iterate an additional cycle for every test that was averaged. I can't figure out why this would be the case. It looks and behaves like an off-by-one erros, but the conditions to close the loop appear satisfied. It can be solved with a 'break', but I'd rather know what the problem is to solve it more organically.
Can anyone let me know why this odd behavior is happening?
CODE:
def scoreAverager():
done=0
scoreTot=0
numScores=0
average=0
while done == 0:
score=input("Enter the numerical score, or enter 'x' to finish entering scores:")
acceptedXs={"X","x"}
if score in acceptedXs:
print ("The average of the scores is: ",average)
#break #this break is necessary for proper function.
done=1
testQuery()
else:
try:
score=float(score)
scoreTot=scoreTot+score
numScores=numScores+1
average=scoreTot/numScores
except ValueError:
print("EXCEPTION: The entry was invalid, please try again.")
def testQuery():
done=0
while done == 0:
moreTests=input("Do you have a set of score to average? Enter 'Yes' or 'No':")
acceptedNos=("No","NO","no")
acceptedYess=("Yes","YES","yes")
if moreTests in acceptedNos:
print("Program Complete.")
done=1
elif moreTests in acceptedYess:
scoreAverager()
else:
print ("ERROR: The entry was invalid. Please try again.")
def main():
testQuery()
main()
EXAMPLE INPUT/OUTPUT:
Do you have a set of score to average? Enter 'Yes' or 'No':Yes
Enter the numerical score, or enter 'x' to finish entering scores:1
Enter the numerical score, or enter 'x' to finish entering scores:2
Enter the numerical score, or enter 'x' to finish entering scores:x
The average of the scores is: 1.5
Do you have a set of score to average? Enter 'Yes' or 'No':Yes
Enter the numerical score, or enter 'x' to finish entering scores:1
Enter the numerical score, or enter 'x' to finish entering scores:2
Enter the numerical score, or enter 'x' to finish entering scores:x
The average of the scores is: 1.5
Do you have a set of score to average? Enter 'Yes' or 'No':No
Program Complete.
Do you have a set of score to average? Enter 'Yes' or 'No':No
Program Complete.
Do you have a set of score to average? Enter 'Yes' or 'No':No
Program Complete.