我正在试着组合一个fx,我让用户输入一个分数列表和要曲线的点数。然后,fx应该将"curve by“的数量添加到每个测试分数,然后返回平均值。在一个fx中,我可以要求等级,amt曲线,并与原始等级列表和添加的点数到每个等级的amt。在第二个fx中,我可以对新列表求和(w/out sum fx -important),然后获取并返回平均值。有没有可能把这些放在一起放在一个fx里?对于这个赋值,我可以使用for/while循环和条件语句。
1)。获取列表并添加积分,以生成新列表
grades = [int(x) for x in input("Please enter the grades separated by a space").split()]
c = float(input("Please enter the number of points to curve by: "))
new = [ ]
def addcurve(grades, c):
for n in grades:
new.append(n+c)
return new
print(addcurve(grades, c))
[OUT]: [94.0, 45.0, 78.0, 95.0, 60.0, 74.0]
2)。对新列表求和,并取平均值
[IN]:
def sumavg(new):
total_sum = 0
for n in new:
total_sum += n
gt = len(new)
final = total_sum/gt
return "%.2f" %(final)
print(“曲折成绩后的新平均值是”,sumavg( new ))
[OUT]: The new average after curving the grades is 74.33
如果有人有什么见解,请让我知道!
谢谢!
干杯,
瑞秋
发布于 2020-11-01 22:37:31
首先,在函数外部声明一个变量是不好的做法,函数用它来存储它的操作结果并返回它。相反,应该在addcurve()
中声明new = []
其次,你想要的函数是:
def foo(grades, c):
total_sum = 0
for n in grades:
total_sum += n+c
final = total_sum/len(grades)
return "%.2f" %(final)
https://stackoverflow.com/questions/64637508
复制