[英]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.