【问题标题】:Counting number of "passes" within a loop计算循环内的“通过”次数
【发布时间】:2016-03-23 21:16:58
【问题描述】:

这是我的代码,简而言之,它从 csv 文件导入数据,读取数据并根据 csv 文件中存储的两个分数确定学生的百分比和成绩。我要做的是查找并存储有多少 A、B、C、D 等级和失败,然后以“有 2 个 A 通过”“有 4 个 B 通过”的方式输出它们等等……

提前感谢您的帮助。

import csv

with open ("UASHSDDP3efiles.csv", "r") as csvfile: #Reads the csv file and puts the names into a list
    reader = csv.reader(csvfile)
    list0 = []
    for row in reader:
        list0.append(row[0])

with open ("UASHSDDP3efiles.csv", "r") as csvfile: #Reads the csv file and puts the prelim marks into a list
    reader = csv.reader(csvfile)
    list1 = []
    for row in reader:
        list1.append(row[1])

with open ("UASHSDDP3efiles.csv", "r") as csvfile: #Reads the csv file and puts the coursework marks into a list
    reader = csv.reader(csvfile)
    list2 = []
    for row in reader:
        list2.append(row[2])

list3 = [(int(x) + int(y)) for x, y in zip(list1, list2)] #Creates a list with each set of marks added together

for i in range(len(list3)): #Creates a loop to go through the list
    totalmark = list3[i] #Takes the first piece of data and calls it totalmarks, on the second loop, it will be the second piece of data and so on...
    percentage = (totalmark / 150) * 100 #Finds the percentage of their total mark out of 150

    if percentage >=  70: #Checks if they have received an A grade
        grade = "A"
    if 60 <= percentage < 70: #Checks if they have received a B grade
        grade = "B"
    if 50 <= percentage < 60: #Checks if they have received a C grade
        grade = "C"
    if 45 <= percentage < 50: #Checks if they have received a D grade
        grade = "D" 
    if percentage < 45: #Checks if they haven't received a grade
        grade = "No grade"

    roundedpercentage = round(percentage) #Rounds the percentage to the nearest integer
    print(list0[i],"'s percentage was", roundedpercentage,"%", "and their grade was:", grade) #Prints the pupils name, percentage and grade

max=max(list3) #Finds the highest mark
print("The highest mark achieved was:", max)
min=min(list3) #Finds the lowest mark
print("The lowest mark achieved was:", min)

这是输出:

>>> Alison Brown 's percentage was 71 % and their grade was: A
Peter Smith 's percentage was 41 % and their grade was: No grade
Katrina Cunningham 's percentage was 60 % and their grade was: B
Jason Talbot 's percentage was 40 % and their grade was: No grade
Shahida Choudry 's percentage was 70 % and their grade was: A
Ian Li 's percentage was 50 % and their grade was: C
Petra Carter 's percentage was 39 % and their grade was: No grade
Hermann Zimmer 's percentage was 69 % and their grade was: B
Tatiana Krystof 's percentage was 60 % and their grade was: B
Oliver Hirschbiegal 's percentage was 49 % and their grade was: D
Lola Portillo 's percentage was 59 % and their grade was: C
Alberto Maura 's percentage was 69 % and their grade was: B
Diana Elliot 's percentage was 25 % and their grade was: No grade
Hilary Clark 's percentage was 49 % and their grade was: D
Ruksana Cabuk 's percentage was 11 % and their grade was: No grade
The highest mark achieved was: 106
The lowest mark achieved was: 17

【问题讨论】:

    标签: list loops csv python-3.x import


    【解决方案1】:

    创建一个包含五个元素的数组 a=np.zeros(5)。在每个年级制作代码

    if percentage >= 70: grade = "A" ind=ord(grade)-ord("A") a[ind]=a[ind]+1
    在其他情况下,即没有等级 制作a[4]=a[4]+1。你会得到一个包含所需答案的数组

    【讨论】:

      【解决方案2】:

      您可以创建一个以成绩为键的字典,并为遇到的每个成绩递增值。 for循环变成(为了可读性做了一些代码简化)

      # create the dict first
      gradescount = dict()
      
      for student, totalmark in zip(list0, list3): # iterate directly over the lists
          percentage = (totalmark / 150) * 100
          if percentage >= 70:
              grade = "A"
          elif percentage >= 60:  #elif prevents extra tests
              grade = "B"
          elif percentage >= 50:
              grade = "C"
          elif percentage >= 45:
              grade = "D"
          else:
              grade = "No grade"
      
          # Increment appropriate grade counter
          gradescount[grade] = gradescount.get(grade, 0) + 1
      
          roundedpercentage = round(percentage)
          print("%s's percentage was %d%% and their grade was: %s" % (
                  student,
                  roundedpercentage,
                  grade
          ))
      

      然后,在你的脚本结束时:

      for grade in ('A', 'B', 'C', 'D'):
          print("There were %d %s passes" % (gradescount.get(grade, 0), grade))
      print("There were %d fails" % (gradescount.get('No grade', 0),))
      

      希望这是你想要的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-12
        • 1970-01-01
        • 2021-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多