+ 2

Problem with class grade analyzer

I typed in the code and made a mistake, so it looks like this: grades = [8.5, 7.9, 6.8, 8.2, 9, 6.3, 8.4] count = 0 # Count the number of grades above 8.0 for grade in grades: print(grades) # Display the result for grade in grades: if grade >= 8.0: print(grade) Is there any help on making this code correct?

16th Aug 2024, 1:52 PM
Tiffany Nguyen
Tiffany Nguyen - avatar
4 Réponses
+ 4
The output should be the number of elements in the grades list whose value is greater than 8.0. for grade in grades: if grade > 8.0: count+=1 #This part of code will count the number of grades greater than 8.0 and stored in the count variable. #Now just output the value of count
16th Aug 2024, 2:39 PM
Gulshan Mahawar
Gulshan Mahawar - avatar
+ 2
Tiffany Nguyen , here your code with some comments how to fix the issues: grades = [8.5, 7.9, 6.8, 8.2, 9, 6.3, 8.4] count = 0 # Count the number of grades *above 8.0* for grade in grades: # *** this part is not required print(grades) # *** this part is not required # Display the result for grade in grades: if grade >= 8.0: # *** should be > 8, not >= count ... # *** counter should be incremented here print(count) # *** after the loop print counter btw: it would be helpful if the tutorial name / lesson name / exercise name is mentioned.
16th Aug 2024, 2:48 PM
Lothar
Lothar - avatar
+ 2
# Hi Tiffany Nguyen, # NumPy is a powerful tool for numerical tasks: import numpy as np LIMIT = 8.0 GRADES = (8.5, 7.9, 6.8, 8.2, 9, 6.3, 8.4) arr_grades = np.array(GRADES) res = np.sum(arr_grades > LIMIT) print( f"For {GRADES}:\n\n" f"\t-> {res} numbers > {LIMIT}" ) # https://sololearn.com/compiler-playground/c1rf8styCw4f/?ref=app
17th Aug 2024, 7:18 AM
Per Bratthammar
Per Bratthammar - avatar
0
grades = [8.5, 7.9, 6.8, 8.2, 9, 6.3, 8.4] count = 0 for grade in grades: if grade > 8.0: count += 1 print(count)
20th Sep 2024, 2:55 PM
Abdoulrazak Omar Bogoreh
Abdoulrazak Omar Bogoreh - avatar