Alex WB
Alex WB

Reputation: 21

Python script to calculate your average class notes

How can I make a program, for a begginer, to calculate my average class notes. I try to do it with def's but i get something like this:

a1 = int(input("How many grades do you have? "))
def media_notelor(a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,v=0,x=0,y=0,z=0):
    x = a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t+u+v+x+y+z
    ma = x/a1
    print(ma)

and then i need a loop? what can i use?

Upvotes: 0

Views: 217

Answers (3)

moh80s
moh80s

Reputation: 761

you could make a list of your grades and then pass it into the following function:

def average(list_of_grades: list):
    summation = sum(list_of_grades)
    num_of_grades = len(list_of_grades)
    return summation / num_of_grades

Upvotes: 1

Nuno Palma
Nuno Palma

Reputation: 524

You could do something like this

grades = []

while True:
    print("Insert your grade or a negative value to finish")
    grade = int(input())

    if grade < 0:
        break

    grades.append(grade)


print("The average is : " + str(sum(grades) / len(grades)))

Upvotes: 1

Reznik
Reznik

Reputation: 2806

You can do it with *args

def my_avg(*my_grades):
    print(float(sum(my_grades)) / len(my_grades)) # calculate avrage


number_of_grades = int(input('how many grades do you have')) # input how many grades do we have
grades = [] # list to store our grades
for i in range(number_of_grades):
    grades.append(int(input('Enter grade'))) # Inserting grades
my_avg(*grades)

Upvotes: 0

Related Questions