I have been working on a test average calculator where the user is supposed to enter any amount of test scores he wants and the number needs to be both an integer (the teacher wants us to use isnumeric) and between 0 and 100. This is what I have so far: print("Welcome to the test average calculator program \n Enter 999 if you have no more test scores")
# Initial Variable Declaration
testScore = 0
testCounter = 0
totalTestScore = 0
testAverage = 0.00
testScoreLimit = 100
# User Input
while testScore != 999:
testScore = input('Please Enter your Test Score: ')
if testScore.isnumeric():
if testScore.isnumeric() <= testScoreLimit:
totalTestScore += testScore.isnumeric()
testCounter += 1
else:
print('Test score is not within the 0-100 range. Please try again.')
else:
print('Not a valid test score. Please try again.')
else:
testAverage = round(totalTestScore/testCounter, 2)
print('Total number of Test Scores entered: ', testCounter)
print(f'Overall Test Score: {testAverage}%')
The problem is that when I attempt to run the code, it does not exit the loop when I enter 999 for testScore nor does it say that the test score is out of range if the test score entered is greater then 100. What am I doing wrong?
Thank you!
Your
testScorevariable is never converted to anint. Useisnumeric()to see if it can be converted to a string, useintto actually convert it to a string.should work if you change it to:
Note that the final
elseblock is unnecessary since you neverbreakyour loop; anelseis only useful as part of a loop if you have something that you specifically want to do only if the loop is never broken. In your case you can just remove the finalelse:and unindent the code underneath it.