In: Computer Science
write a program that:
1) asks the user to enter grades, a negative number is the signal to stop entering. (while loop) - add each grade to a list (do not add the negative number to the list)
after the while loop to enter grades is finished:
2) use a for loop on the list to calculate the average of all numbers in the list
3) use a for loop on the list to find the largest number in the list
4) use a for loop on the list to find the smallest number in the list
your program should have 1 while loop and 3 for loops
python code
Python code:
#initializing a grades list
grades=[]
#accepting the grade
num=int(input("Enter the grade: "))
#looping till a negative number is entered
while(num>=0):
#adding it to list
grades.append(num)
#accepting the grade
num=int(input("Enter the grade: "))
#initializing sum as 0
sum=0
#looping all grades
for i in grades:
#adding current number to sum
sum+=i
#printing Average
print("Average:",sum/len(grades))
#initializing max as 0
max=grades[0]
#looping all grades
for i in grades:
#checking if the current number is the maximum
if(i>max):
#assigning current number as maximum
max=i
#printing Largest number
print("Largest number:",max)
#initializing min as 0
min=grades[0]
#looping all grades
for i in grades:
#checking if the current number is the minimum
if(i<min):
#assigning current number as minimum
min=i
#printing Smallest number
print("Smallest number:",min)
Screenshot:
Input and Output: