In: Computer Science
In Python: read in 'marks.txt' . The file format is one number per line. Your program will run through all these numbers, and make sure that they are valid input.
you have to Create two lists: one for valid numbers and one for invalid numbers.
For a number to be valid the number has to:
Must be less than or equal to 100
Must be greater than or equal to 0
Must not have any letters inside of it (it must be numeric -- use the string function .isnumeric())
Print out list of bad inputs, and print out average of the good inputs.
thank you
lst_valid=[]
lst_Invalid=[]
for line in open("marks.txt"):
line = line.strip()
try:
temp = int(line)
if temp >= 0 and temp <=100:
lst_valid.append(temp)
else:
lst_Invalid.append(temp)
except:
lst_Invalid.append(line)
print("Valid:",lst_valid)
print("InValid:",lst_Invalid)
if len(lst_valid) != 0:
print("Average of Valid Numbers:",sum(lst_valid)/len(lst_valid))
else:
print("Average of Valid Number is 0")
---------------------------------------------------
SEE OUTPUT
Thanks, PLEASE COMMENT if there is any concern.