Reputation: 69
I am writing a program for a course that takes user input exam scores and averages them. I have a functioning program, however I need to add a function that uses a range to identify scores entered outside of the 1-100 range and display a message "score out of range. please re enter".
Here is what I have so far:
SENTINEL = float(9999)
scores = []
while True:
number = float(input("Enter exam score (9999 to quit): "))
if number == SENTINEL:
break
scores.append(number)
if not scores:
print("No scores entered")
else:
avg = sum(scores)/len(scores)
print("average of ", len(scores), " test scores is :", avg)
Upvotes: 0
Views: 84
Reputation: 513
Try this:
SENTINEL = float(9999)
scores = []
while True:
number = float(input("Enter exam score (9999 to quit): "))
while number != SENTINEL and (number < 1 or number > 100):
number = float(input("score out of range. please re enter: "))
if number == SENTINEL:
break
scores.append(number)
if not scores:
print("No scores entered")
else:
avg = sum(scores)/len(scores)
print("average of ", len(scores), " test scores is :", avg)
Upvotes: 1