strScore
is a string. Your while
loop should look like this:
while strScore != "done":
try:
score = int(strScore)
if score >= 0 and score <= 10:
totalScore = totalScore + score
hwCount = hwCount + 1
else:
print("Please enter a number between 0 and 10")
except ValueError:
print("Please enter only whole numbers or 'done'")
strScore = input("Enter HW#" + str(hwCount) + " score: ")
As you show in your code, there are three cases to handle - user enters a valid score, user enters a valid number but invalid score, and user enters an invalid number. If the user enters an invalid integer, trying int(strScore)
will raise a ValueError
, which we can catch and report. Knowing that otherwise score
will be a valid int
, we only have to check if it is a valid score or not, allowing you to change the elif
into a simple else
.