1

I'm fairly new to programming and have only been doing so for a month. Currently I'm trying to get user input, store it in a list, and pass that list into a function. I'm having trouble using the list as an argument for my function (the last line of code). Thank in advance!

grade_list = []
percentages = 0

while True:
    percentages = input("Enter some numbers here: ")
    if percentages == "done":
        break
    grade_list.append(percentages)

print(grade_list)


def gpaCalc(marks):
    gpaList = []
    for grade in marks: #sorts data
        if grade <= 49.99:
            grade = 0.00

        elif 50 <= grade <= 52.99:
            grade = 0.70

        elif 53 <= grade <= 56.99:
            grade = 1.00

        elif 57 <= grade <= 59.99:
            grade = 1.30

        elif 60 <= grade <= 62.99:
            grade = 1.70

        elif 63 <= grade <= 66.99:
            grade = 2.00

        elif 67 <= grade <= 69.99:
            grade = 2.30

        elif 70 <= grade <= 72.99:
            grade = 2.70

        elif 73 <= grade <= 76.99:
            grade = 3.00

        elif 77 <= grade <= 79.99:
            grade = 3.30

        elif 80 <= grade <= 84.99:
            grade = 3.70

        elif 85 <= grade <= 89.99:
            grade = 3.90

        elif 90 <= grade <= 100:
            grade = 4.00

        gpaList.append(grade) #gathers data into list
        gpaList.sort()

    return gpaList

print (gpaCalc(PROBLEM))
5
  • gpaCalc(grades_list) Commented Sep 6, 2015 at 3:08
  • What error are you getting? Commented Sep 6, 2015 at 3:19
  • are you converting your inputs to floats? doesn't look that way to me, looks like you are keeping them as strings. i.e. if '48' <= 49.99 Commented Sep 6, 2015 at 3:32
  • hey @JLPeyret , I just realized that and made the correction, however "done" can't be converted to a float. Is there another way to break away from user input? Commented Sep 6, 2015 at 3:37
  • @xdhmoore, I was getting "TypeError: unorderable types: str() <= float()". Then I added in a float, but now the "done" string to abort the loop can't be converted and I'm getting "alueError: could not convert string to float: 'done'" Commented Sep 6, 2015 at 3:41

3 Answers 3

1

You can pass a list as you would pass it normally into any function, just always make sure you are accessing the items in the list by indexing correctly, rather than calculating the whole list. Use the following instead:

def gpaCalc(marks):
    gpaList = []
    for grade in marks[0]: #sorts data

        if grade <= 49.99:
            grade = 0.00

        elif 50 <= grade <= 52.99:
            grade = 0.70

        elif 53 <= grade <= 56.99:
            grade = 1.00

        elif 57 <= grade <= 59.99:
            grade = 1.30

        elif 60 <= grade <= 62.99:
            grade = 1.70

        elif 63 <= grade <= 66.99:
            grade = 2.00

        elif 67 <= grade <= 69.99:
            grade = 2.30

        elif 70 <= grade <= 72.99:
            grade = 2.70

        elif 73 <= grade <= 76.99:
            grade = 3.00

        elif 77 <= grade <= 79.99:
            grade = 3.30

        elif 80 <= grade <= 84.99:
            grade = 3.70

        elif 85 <= grade <= 89.99:
            grade = 3.90

        elif 90 <= grade <= 100:
            grade = 4.00

        gpaList.append(grade) #gathers data into list
        gpaList.sort()

    return gpaList

grade_list = []
percentages = 0

while True:
    percentages = input("Enter some numbers here: ")
    if percentages == "done":
        break
    grade_list.append(percentages)

print(gpaCalc(grade_list))
Sign up to request clarification or add additional context in comments.

Comments

1

keep your check for "done" as is. if it is not done, then convert float.

while True:
    percentages = input("Enter some numbers here and 'done' to exit:")
    if percentages == "done":
        break

    try:
        grade_list.append(float(percentages))
    except ValueError:
        pass

sorting...

    for grade in marks: #sorts data
        .....

        gpaList.append(grade) #gathers data into list

    #also, sort outside the loop, when done, not each time.
    gpaList.sort()

    return gpaList

1 Comment

Thanks a lot! I haven't encountered error handling yet, I guess ill spend a few more days reading python books. This did the trick.
1

Right before the last print line, define your list of marks, e.g. marks = [70, 68, 50, 89, ...] and pass it to gpaCalc in your function call:

print(gpaCalc(marks))

Note that Python convention says you should not use camel case in your identifiers; use underlines instead: gpa_calc

Edit: I missed the point of the question! To get the user's inputs, use a loop:

def get_user_input():
    grades = []

    while True:
        # take input
        value = ... # figure it out

        if value == 'q':
            break

        try:
            # do basic validation here
            grades.append(int(value))

            # might be a good idea to check the range too…
        except ValueError:
            print("This is not a valid grade!")

    return grades

If you would like an explanation, leave a comment!

3 Comments

agree. separate out the user input logic from the classification function. start out fixing something like gpaCalc([48.0, 31,95]) then make sure the user can input the same terms and get the same results (might need to allow for typos resulting in float conversion errors).
Thanks, ill keep that in mind! But for the list, I was hoping to get user input and have that data operated on.
yes, but solving two problems at a time is not an easy approach. hardcode some valid values, and get gpaCalc to work first. actually, this is where Python's unittest module shines (diveintopython.net/unit_testing/romantest.html). then type and retype the user input manually until your user input formatting works as well. otherwise, you can look forward to lots of typing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.