How to get average, highest and lowest values of input values

Write a python program which can store the name of the student and their exam score. The program should be able to:

  1. Calculate the average score for the students and print the average scores once you finish entering the student names and scores.
  2. Summarize who get the highest and lowest.

I got stuck at how to find average and highest and lowest, this is my code :

students = {}

polling_active = True

while polling_active:
    name = input("enter your name: ")
    score = int(input("enter your score: "))
    
    students[name] = score
    
    repeat = input("would you like to add another student?" "(yes/no)")
    if repeat == 'no':
        polling_active = False
        
print ("-------RESULT-------")
for name, score in students.items():
    print(name, "your score is: ", score )
    
total_sum = float(sum(score)) 
print (total_sum)

Upvotes: 1

Views: 6598

Answers (5)

Aura
Aura

Reputation: 35

You can directly set your while loop as True to save the trouble of making another variable:

while True:

and break it with

    name=input("Enter your name: ")
    if name=='no':
        break

and to find your average you could do

average=(sum(students.values()))/len(students)

then finally for highest and lowest you can use the in built max and min functions as so

highest=max(students.values())
lowest=min(students.values())

Upvotes: 0

Naaman
Naaman

Reputation: 62

I guess this may be helpful

student = []
student_1=[]
score = []
copy = [] 
loop = True
yes = ["YES","yes","y","Y"]
while loop:
    Name = input("Name : ")
    Score = int(input("Score : "))
    student.append(Name)
    score.append(Score)
    copy.append(Score)
    Enter_more = input("Write YES or Y or y or yes to add more names: ")
    if Enter_more in yes:
        continue
    else:
        break
score.sort()
score.reverse()
sum = 0
for i in range(len(score)):
    index = score[i]
    student_1.append(student[copy.index(index)])
    sum+=score[i]
    score[i] = student[i]
print("Average Score is "+str(float(sum/len(score))))
print("Highest Score is Scored by  "+str(student_1[0]))
print("Lowest Score is Scored by "+str(student_1[len(student_1)-1]))

Upvotes: 0

Kashem
Kashem

Reputation: 125

There are many possible solutions to this problem. I will describe elaborately.

import sys
students = {}

polling_active = True

while polling_active:
    name = input("enter your name: ")
    score = int(input("enter your score: "))
    
    students[name] = score
    
    repeat = input("would you like to add another student?" "(yes/no)")
    if repeat == 'no':
        polling_active = False
        
print ("-------RESULT-------")
#We will use totalScore for keeping all students score summation.
#Initaly total score is 0.
totalScore = 0
#For getting lowest score student name, we need lowest score first. 
#Initaly we don't know the lowest score. So we are assuming lowest score is maximum Int value
lowestScore = sys.maxsize 
#We also need to store student name. We can use lowestScoreStudentName variable for storing student name with lowest score
#Initaly we don't know the student name. So we initialize it as an empty student list.
lowestScoreStudentName = []
#For getting maximum score student name, we need maximum score first. 
#Initaly we don't know the maximum score. So we are assuming lowest score is minimum Int value
maximumScore = -sys.maxsize - 1
#We also need to store student name. We can use maximumScoreStudentName variable for storing student name with maximum score
#Initaly we don't know the student name. So we initialize it as an empty student list.
maximumScoreStudentName = []

for name, score in students.items():
    totalScore = totalScore + score
    print(name, "your score is: ", score )
    if lowestScore > score:
        lowestScore = score
        #Making student list empty, since score is lower than before
        lowestScoreStudentName = []
        lowestScoreStudentName.append(name)
    elif lowestScore == score:
        #keeping all students in the list who gets lowest score
        lowestScoreStudentName.append(name)
        
    if maximumScore < score:
        maximumScore = score
        #Making student list empty, since score is higher than before
        maximumScoreStudentName = []
        maximumScoreStudentName.append(name)
    elif maximumScore == score:
        #keeping all students in the list who gets highest score
        maximumScoreStudentName.append(name)
    
total_sum = sum(list(students.values()))
average_sum = totalScore/len(students) 

print("Average score : " + str(average_sum))
print("Lowest Score holder students name: " + str(lowestScoreStudentName))
print("Highest score holder students name: " + str(maximumScoreStudentName))

Upvotes: 0

Ali Hassan
Ali Hassan

Reputation: 1

students = {}

def Average(lst): 
    return sum(lst) / len(lst)

while True:
    name = input("enter your name: ")
    score = int(input("enter your score: "))

    students[name] = score

    repeat = input("would you like to add another student?" "(yes/no)")
    if repeat == 'no':
        break
    
print ("-------RESULT-------")
for name, score in students.items():
    print(name, "your score is: ", score )




avg = Average(students.values())

highest = max(students,key=students.get)

lowest = min(students,key=students.get)

print ('avg :',avg)
print ('highest :',highest)
print ('lowest :',lowest)

Upvotes: 0

IoaTzimas
IoaTzimas

Reputation: 10624

Continue your code with the following, to get the average and max-min:

total_sum = sum(list(students.values()))/len(students)
highest=max(students, key=lambda x:students[x])
lowest=min(students, key=lambda x:students[x])

print('Average: ', total_sum)
print('Highest score: ', highest, '   ',students[highest])
print('Lowest score: ', lowest, '   ',students[lowest])

Upvotes: 0

Related Questions