简体   繁体   English

如何使用python在函数中使用列表作为参数

[英]how do I use a list as an argument in a function using python

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))

Right before the last print line, define your list of marks, eg marks = [70, 68, 50, 89, ...] and pass it to gpaCalc in your function call: 在最后一个print行之前,定义标记列表,例如marks = [70, 68, 50, 89, ...] 70,68,50,89 marks = [70, 68, 50, 89, ...]并将其传递给函数调用中的gpaCalc:

print(gpaCalc(marks))

Note that Python convention says you should not use camel case in your identifiers; 请注意, Python约定表明您不应在标识符中使用驼峰大小写; use underlines instead: gpa_calc 使用下划线代替: 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! 如果您想要解释,请发表评论!

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))

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 function 中使用方程作为参数? - How do I use an equation as an argument in a function? 如何在python函数中更改参数? - How do I change an argument in a python function? 如何使用 for 循环 append python 中的列表并替换 function? - How do I append a list in python using a for loop and replace function? 我如何在Python中执行此操作? 列表功能 - How do I do this in Python? List to function (Python 2.7)使用列表作为函数中的参数? - (Python 2.7) Use a list as an argument in a function? Python:使用列表索引作为 function 参数 - Python: use a list index as a function argument 我如何使用类型来强制使用特定的枚举作为 python 中的参数 - How do I use typing to force using a specific enum as an argument in python Python - 使用随机播放时参数“函数”如何影响列表 - Python - How does argument 'function' affect a list when using shuffle 如何在 Python 中使用辅助函数作为 typing.Callable 参数和 playwright page.wait_for_event 函数? - How do I use a helper function as typing.Callable argument with the playwright page.wait_for_event function in Python? Python:如何在用作参数本身的函数中使用参数? - Python: How can I use arguments in a function used as argument itself?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM