Reputation: 1
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!
Upvotes: 0
Views: 42
Reputation: 71454
Your testScore
variable is never converted to an int
. Use isnumeric()
to see if it can be converted to a string, use int
to actually convert it to a string.
while testScore != 999:
testScore = input('Please Enter your Test Score: ')
if testScore.isnumeric():
if testScore.isnumeric() <= testScoreLimit:
totalTestScore += testScore.isnumeric()
should work if you change it to:
while testScore != "999":
testScore = input('Please Enter your Test Score: ')
if testScore.isnumeric():
if int(testScore) <= testScoreLimit:
totalTestScore += int(testScore)
Note that the final else
block is unnecessary since you never break
your loop; an else
is 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 final else:
and unindent the code underneath it.
Upvotes: 0