对于初学者,我如何制作一个程序来计算平均课堂笔记。 我尝试用def来做到这一点,但是我得到了这样的东西:
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)
然后我需要循环吗?我该怎么用?
答案 0 :(得分:1)
您可以这样做
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)))
答案 1 :(得分:1)
您可以列出您的成绩列表,然后将其传递给以下功能:
def average(list_of_grades: list):
summation = sum(list_of_grades)
num_of_grades = len(list_of_grades)
return summation / num_of_grades
答案 2 :(得分:0)
您可以使用*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)