Hello, so I made a code to find calculate the average total mark from the user's inputs. So they choose how much marks they want to input but like when a number less than 0 or more than 100, it still counts for one of the inputs, but doesn't add it for the total mark average. For example if I say I want to input 10 different marks, and I input a number like -5, it counts for one of the different marks and I have 9 inputs left. Please help.
Initialize variables
totalMarks = 0
validMarks = 0
Prompt user for number of marks
numMarks = int(input("Enter the number of marks: "))
If number of marks is less than 0, print error message
if numMarks <= 0:
print("Please enter valid mark number of marks")
else:
Input marks and calculate total
for mark in range(numMarks):
mark = float(input("Enter mark: "))
if 0 <= mark <= 100:
# Add the mark to the total
totalMarks += mark
# Add the number of valid marks
validMarks += 1
else:
print("Error: Invalid mark. Please enter a mark between 0 and 100.")
if validMarks <= 0:
print("No valid marks entered.")
else:
average = totalMarks / validMarks
print("The average mark is: {:.1f}%".format(average))