I'm trying to make a grade average system but with while loops, how can I individually get the outputs from the while loop?
Question:
Answers:
Can you add any more detail, or an example of the loop(s) in question? It looks like you meant to after "…came up so far:"?
Okay. I understand a little better. I have to run in a minute, but, I’ll try to point you in the right direction…
You will need to use a list to collect your scores. For example, before your while loop:
score_list = []
While……input grade
score_list.append(grade)
After the While loop…
avg_grade = sum(score_list) / len(score_list)
You may create an empty list before the loop and then append each entered value to it within a loop:
unit = 0
grades = []
while unit < 8:
grade = int(input("Enter Grades: "))
grades.append(grade)
unit += 1
Then you will be able to access any value from the list:
print(grades[0])
If you’re trying to store multiple grades, and perform operations on them, then a list can help. You can define "grades" as a list variable before the while loop. Then after the loop you can use print() function to see what’s in the list.
Using a for loop, you can perform operations with each grade in the list. To get the sum of the grades you can define a "sum" variable and add each grade from the for loop.
Based on what you’ve said, I think this is a good starting point:
EDIT: Changed variable "sum" to "g_sum" because sum is a built-in Python method. Using a variable by the same name could pose a problem if you tried to use this method.
unit = 0
grades = [] # this is the list of grades
while unit < 8:
# append will add each grade to the list
grades.append(int(input("Enter Grades: ")))
unit += 1
# printing all values in the list
print(grades)
# we can already get a denominator from the length of the list
denominator = len(grades)
# for loop to get sum of all grades in list
g_sum = 0
for g in grades:
g_sum += g
# now we can get the average
average = g_sum / denominator
# printing sum and average of grades
print(f"Sum of grades: {g_sum}nAverage of grades: {average}")
def get_average():
count = 0
total = 0
while count < 3:
grade = float(input('Enter grade: '))
count += 1
total += grade
average = total / count
print(average)
Can you add any more detail, or an example of the loop(s) in question? It looks like you meant to after "…came up so far:"?
Okay. I understand a little better. I have to run in a minute, but, I’ll try to point you in the right direction…
You will need to use a list to collect your scores. For example, before your while loop:
score_list = []
While……input grade
score_list.append(grade)
After the While loop…
avg_grade = sum(score_list) / len(score_list)
You may create an empty list before the loop and then append each entered value to it within a loop:
unit = 0
grades = []
while unit < 8:
grade = int(input("Enter Grades: "))
grades.append(grade)
unit += 1
Then you will be able to access any value from the list:
print(grades[0])
If you’re trying to store multiple grades, and perform operations on them, then a list can help. You can define "grades" as a list variable before the while loop. Then after the loop you can use print() function to see what’s in the list.
Using a for loop, you can perform operations with each grade in the list. To get the sum of the grades you can define a "sum" variable and add each grade from the for loop.
Based on what you’ve said, I think this is a good starting point:
EDIT: Changed variable "sum" to "g_sum" because sum is a built-in Python method. Using a variable by the same name could pose a problem if you tried to use this method.
unit = 0
grades = [] # this is the list of grades
while unit < 8:
# append will add each grade to the list
grades.append(int(input("Enter Grades: ")))
unit += 1
# printing all values in the list
print(grades)
# we can already get a denominator from the length of the list
denominator = len(grades)
# for loop to get sum of all grades in list
g_sum = 0
for g in grades:
g_sum += g
# now we can get the average
average = g_sum / denominator
# printing sum and average of grades
print(f"Sum of grades: {g_sum}nAverage of grades: {average}")
def get_average():
count = 0
total = 0
while count < 3:
grade = float(input('Enter grade: '))
count += 1
total += grade
average = total / count
print(average)