我已经有一段时间了,但我不太清楚如何解决它。我有三个数据集 - 得分(AF,计算每个等级的成绩数),计数(程序按等级打印计数表),百分比(将计数除以数据集中的总数以获得每个等级中的百分比)。
最终结果应该与给定的7个等级(整数)类似。 (示例用户输入数据:[88,76,89,78,74,87,95])
>>> GRADE COUNT PERCENTAGE
>>> A 2 28.6%
>>> B 2 28.6%
>>> C 3 42.9%
>>> D 0 0%
>>> F 0 0%
目前,这是我冗长乏味的基本代码。我很难执行并使其正常工作,我可能根本没有让它正常工作。
def grade_scores(scoreList):
for score in scoreList: #shows the scores in the score list, uses ddata from previous function
if score >= 91:
Ascore = 'A'
Acount +=1
sum(Ascore) / Acount len(scoreList) = Apercentage
elif score >= 81 and score <=90:
Bscore = 'B'
Bcount +=1
sum(Bscore) / Bcount len(scoreList) = Bpercentage
elif score >= 71 and score <=80:
Cscore = 'C'
Ccount +=1
sum(Cscore) / Ccount len(scoreList) = Cpercentage
elif score >= 61 and score <=70:
Dscore = 'D'
Dcount +=1
sum(Dscore) / Dcount len(scoreList) = Dpercentage
else:
Fscore = 'F'
Fcount +=1
sum(Dscore) / Dcount len(scoreList) = Dpercentage
for i in range(scoreList):
print ('Grade', '\t', 'Count', '\t', 'Percentage')
print(Ascore, end="\t", Acount, end="\t", Apercentage)
print(Bscore, end="\t", Bcount, end="\t", Bpercentage)
print(Cscore, end="\t", Ccount, end="\t", Cpercentage)
print(Dscore, end="\t", Dcount, end="\t", Dpercentage)
print(Fscore, end="\t", Fcount, end="\t", Fpercentage)
我希望更多的是网格样式,但我不认为它格式正确,似乎没有正常工作(我也在第6行得到关于“len”的错误)。我很感激对此的任何意见。
答案 0 :(得分:0)
不完全确定你的意思是什么,但首先,错误信息很可能是由TypeError引起的,因为你在range()函数中使用变量scoreList(它接受整数,而整数没有长度)。
此外,您可能需要考虑使用列表来更好地整理数据。
对于网格样式,您应该使用.format()方法来帮助您打印行:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="b1">site1</button>
<button id="b2">site2</button>
<iframe id="target"></iframe>
对于elif条件,你必须坚持你已有的。但是,您可能希望稍微修改变量名称,因为更好的方法是通过列表:
# Assuming you have 3 lists:
# grade = ['A','B','C','D','E']
# count = [2, 2, 3, 0, 0] Have another function capture user input
# and convert it into this list.
# percentage = [28.6, 28.6, 42.9, 0, 0] Same as above ^
print("SCORE COUNT PERCENTAGE")
for i in range(len(grade)):
print(" {} {} {} %".format(grade[i], count[i], percentage[i]))
最后,您可以使用另一个for循环来计算平均值:
for i in range(len(gradeInputList)):
if grade >= 90:
count[0] += 1 # Increase index 0 (Corresponds to value A) by 1
elif grade >= 80 and < 90:
count[1] += 1
# Omitted code here, you get the idea
# ...
else:
count[4] += 1
哇,希望这有帮助! :)
答案 1 :(得分:0)
如果要以网格格式打印,请使用制表包。它简单方便。以下是示例代码:
from tabulate import tabulate
table = [["spam",42],["eggs",451],["bacon",0]]
headers = ["item", "qty"]
print tabulate(table, headers, tablefmt="grid")
打印:
+--------+-------+
| item | qty |
+========+=======+
| spam | 42 |
+--------+-------+
| eggs | 451 |
+--------+-------+
| bacon | 0 |
+--------+-------+