-2

I am working on Python and am writing a program where the user inputs how many courses they would like to calculate. Then the program is supposed to take the appended items (the strings) and then divide them by how many courses they would like, in other words the total (integer). I cannot seem to figure out a way to implement this properly, any help? The issue is under If value = 1.

if (value == 1):
    selection = int(input("How many classses would you like to include?\n"))
    for i in range (0,selection):
        print("What is the grade of the class?")
        item = (input())
        grades.append(item)
        GPA_list = [sum(item)/selection for i in grades]
        print(GPA_list)
    
  • 2
    What sorts of strings is the user entering? Letter grades? Numeric scores? You can't divide the letter D by a number. – Samwise Nov 22 '21 at 15:32
  • `item` is probably supposed to be a `float`, not a `str`. – chepner Nov 22 '21 at 15:33
  • why don't you convert the items in to floats? – 5idneyD Nov 22 '21 at 15:33
  • 2
    Yep, you're running `sum` on `item`, but `itemm` is a string. Also, you should probably build the list outside the `for` loop once you have all the items appended to it. – michjnich Nov 22 '21 at 15:34
  • The user is supposed to enter numeric scores, how would I go about converting item from a string to a float, I tried float(input()), but it says that float object is not interable – jfcrespo Nov 22 '21 at 15:35
  • What should the sum over a single float (represented as string) mean? – Michael Butscher Nov 22 '21 at 15:39
  • There is already an empty list, outside of the for loop called grades=[], the item is just a placeholder name for the numeric grade that the user is supposed to input, and selection is the number of courses specifically. – jfcrespo Nov 22 '21 at 15:40
  • sum is used for list only. I think you used wrong variable in sum use sum(grades) instead. – Msvstl Nov 22 '21 at 15:41
  • I used sum(grades), but it only divides the first integer of the group. For example, if you say 2 classes in selection input, then you say the first course is a 99, it will print 49, which is correct, however if for the next course the numbered grade is 99, it will print [98.5, 98.5] for both of them, which is incorrect. – jfcrespo Nov 22 '21 at 15:47
  • Actually, I just realized that the output is correct, however it is printing the sum number as given in selection, would it be possible to just print out just one of the numbers from a list? – jfcrespo Nov 22 '21 at 15:50
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Nov 26 '21 at 08:00

1 Answers1

0

You can simplify this quite a bit by using mean, which does the summing and dividing for you:

>>> from statistics import mean
>>> print(mean(
...     float(input(
...         "What is the grade of the class?\n"
...     )) for _ in range(int(input(
...         "How many classes would you like to include?\n"
...     )))
... ))
How many classes would you like to include?
5
What is the grade of the class?
4
What is the grade of the class?
3
What is the grade of the class?
4
What is the grade of the class?
2
What is the grade of the class?
4
3.4

To fix your existing code, all you need to do is make sure to convert item to a float and then call sum on grades rather than each item:

grades = []
selection = int(input("How many classses would you like to include?\n"))
for i in range(0, selection):
    print("What is the grade of the class?")
    item = float(input())
    grades.append(item)
    GPA_list = sum(grades) / selection
    print(GPA_list)

Note that your code prints a fraction of the average at each step in the loop until finally printing the correct result in the last iteration; if you want to fix this as well, unindent the last two lines.

Samwise
  • 68,105
  • 3
  • 30
  • 44