Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

How to append input value into list?

Good morning,

I'm trying to build a grading system as an outside project. I can't seem to figure out how to append the input value that the user inputs after choosing what category they want to input in. (if block at the bottom). If anyone could please look it over and try to help me out that'd be awesome! Thanks!

import math

#Function that gets the average
def Average(homework):
    return sum(homework) / len(homework)


homework = [100, 95, 100, 90]
average1 = Average(homework)
print("Average of homework =", round(average1, 2))

classwork = [80, 100, 75, 80]
average2 = Average(classwork)
print("Average of classwork =", round(average2, 2))

assessments = [95, 67, 82]
average3 = Average(assessments)
print("Average of assesments =", round(average3, 2))

print("-----------------------------")

additional = input("Would you like to insert a grade?  ")
if additional == 'yes':
    category = input("Please choose a category :  ")
    if category == 'homework' or  'classwork' or 'assesments':
        input = input("{} grade you wish to input :  ".format(category))

        # Figure out how to append input value into the category they select

    else:
        print("That is not a valid category")


#Adding weights to each category (10%, 30%, 60%)
weightedAverage = average1 * 0.1 + average2 * 0.3 + average3 * 0.6
print("-----------------------------")
print("Your overall grade is : ", round(weightedAverage, 2))

4 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,731 Points

Is this what you were thinking about? I took the liberty of modifying and moving around your code a little to improve flow.

The major addition is a dictionary called "gradebook" which references the arrays you declared to a category. I also added a "categories" variable that turns the keys into a list, which allows easier checking.

If you want to see a working version, go to this URL (and press RUN at the top)

https://repl.it/@Nomon/UtilizedSnowRecovery

import math

#Function that gets the average
def Average(homework):
    return float(sum(homework)) / len(homework)

# grades
homework = [100, 95, 100, 90]
classwork = [80, 100, 75, 80]
assessments = [95, 67, 82]

# added this below
gradebook = {'homework': homework, 'classwork': classwork, 'assessments': assessments}
categories = list(gradebook.keys())

# added loop to allow multiple grade additions
print("------------Additional Grades-----------------")
while True:
    additional = input("Would you like to insert a grade?  ")
    if additional == 'yes':
        # notice we can easily use "categories"
        category = input("Please choose a category {}:  ".format(categories))
        if category in categories:
            # print grades in category
            print("current grades in {}: {}".format(category, gradebook[category]))
            score =  input("{} grade you wish to input (must be a number) :  ".format(category))
            # convert the score from string to a number
            try:
              score = float(score)
              # notice we add the score to the gradebook category.
              gradebook[category].append(score)
            except:
              print("Error: score must be a valid number.")
        else:
            print("That is not a valid category")
    else:
        # this causes us to exit the loop
        break


average1 = Average(homework)
print("Average of homework =", round(average1, 2))

average2 = Average(classwork)
print("Average of classwork =", round(average2, 2))

average3 = Average(assessments)
print("Average of assesments =", round(average3, 2))

#Adding weights to each category (10%, 30%, 60%)
weightedAverage = average1 * 0.1 + average2 * 0.3 + average3 * 0.6
print("-----------------------------")
print("Your overall grade is : ", round(weightedAverage, 2))

Jeff Muday

This is why I love the Treehouse community & staff! Thanks, Jeff for taking the time to re-work my code. Your new version is awesome and is what I wanted it to initially do! I am going to implement your code and keep playing around with it and add some more features and whatnot.

Thanks for the learning experience! :))

Happy coding!!

the error saying that category is a string which it is. your logic is incorrect. you need to define the category as a list, crete an empty list. category =[] and then u can append inputs to it.

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,731 Points

Luka,

It was very easy to add to your code because you did a good job of using logical names and organizing in a way that makes sense to a reader. Keep that up and you will go far!

best, Jeff

You could use .append(), in this case: category.append(input)

I've already tried that. This is the attribute error I get :

AttributeError: 'str' object has no attribute 'append'