-2

I'm in my 101 level class and the instructions that I am operating under dictate that I have a parallel lists. One list will be student name and the second will be student test scores. The goal is to average four test scores per student and issue a letter grade. I've seen numerous topics here regarding passing lists as arguments to functions but so far I haven't seen any that address how to have a function only use a set number of elements of a list per iteration.

Now my question, I am writing a function to average four test scores per student (5 total students). The scores are stored in a list of 20 (one of the two parallel lists that I have to have with the other being the student names). How, or can I, write the code to only look at four list elements per iteration (5 total iterations) and average those?

user3797419
  • 3
  • 1
  • 3
  • 3
    please add code of what you have tried so far. – Saimadhav Heblikar Jul 17 '14 at 15:09
  • Or if not the code you've tried, at least the code you've been given to work from. How do those scores/names lists look? Since you have 5 students, are polling 4 scores for each student, and there are 20 total scores.... – Adam Smith Jul 17 '14 at 15:10
  • [chunk](http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python) the list into 4-sized bites, and iterate through those. – Kevin Jul 17 '14 at 15:13

2 Answers2

0

I'm not sure of what you are asking.

There's many way to do something like this:

for n in xrange(4):
    do_something_with(n)

for i, m in enumerate(list_of_elements):
    do_something_with(m)
    if i == 4:
        break
f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
0

I believe what is meant is the code below.. for example...

Students = ["student1", "student2", "student3", "student4", "student5"]
Scores = [12,13,8,15,16,19,18,14,3,15,6,3,9,11,13,5,2,18,3,7]

how can we "for-loop" through the list Scores, and break each four elements and calculate the average of the corresponding student...

why not just loop through the student ????

Students = ["student1", "student2", "student3", "student4", "student5"]
Scores = [12,13,8,15,16,19,18,14,3,15,6,3,9,11,13,5,2,18,3,7]

c = 0
Averages = []

for i in range(5):
    s = 0
    for j in range(4):
        s = s + Scores[c]
        c = c+1
    Averages.append(float (s)/4)

print Averages

Result would be:

[12.0, 16.75, 6.75, 9.5, 7.5]

On the other hand, if you want to solve it with only one "for = loop", you can do below:

Students = ["student1", "student2", "student3", "student4", "student5"]
Scores = [12,13,8,15,16,19,18,14,3,15,6,3,9,11,13,5,2,18,3,7]


Averages = []
s = 0
for i in range(20):
    if ((i) % 4 == 0 and i > 0):
        Averages.append(float(s) / 4)
        s = 0
    s = s + Scores[i]

print Averages
mlwn
  • 1,156
  • 1
  • 10
  • 25
  • Sorry I haven't added code but I am that lost! This seems to be pretty dang close to what I need to be doing. Unfortunately there are elements here that I am unfamiliar with. The 'c', the 'i', the 'j', and the 's' clearly mean something. Sorry, I'm not that advanced and I want to be on your level. Would you please explain what is taking place here? – user3797419 Jul 17 '14 at 15:45
  • I think you need the "edit" I made to the code.. check it please... sorry for not making comments.. I thought they could be clear.. s is the sum of four consecutive scores, i is an integer counter , Averages is a list holding the 5 averages of the 5 students.. – mlwn Jul 17 '14 at 15:51
  • The light bulb just clicked on. That makes sense now. Unfortunately our book doesn't tell us how to use a range function within a list. I assume you can use that function about anywhere right? – user3797419 Jul 17 '14 at 15:55
  • range is very essential in python.. please check the python documentation online.. it is more complete than any book... Don't forget to upvote, or mark as answer in case you are satisfied ! – mlwn Jul 17 '14 at 15:59
  • So, when I utilize the code above I get two errors. First, on the 'Print ""', I get invalid syntax. I suppose I could understand that because I'm not telling the code what to print, just providing quotes marks. Second, when I comment out the previous statement it throws another fault for the 's = s + Scores[i]'. It states that Scores is not defined. As I read the code with my inexperienced eyes, I disagree. I see that Scores is defined. Clearly I'm wrong but where? Where am I going wrong on these two faults? – user3797419 Jul 18 '14 at 08:42
  • which version of python you are running ? and on which platform please ? ... and there is no 'Print ""'.... I have modified the code later... – mlwn Jul 18 '14 at 15:25
  • My bad. You certainly did. I am using Win 7 in a VM and running Python 3.4.1. Even when I edit the print statements out I still get "s = s + Scores[i] NameError: name 'Scores' is not defined" – user3797419 Jul 18 '14 at 15:38
  • It appears that I'm getting the exact same error in both examples. I've tried everything I can think of and some stuff that I found on the internet to no avail. – user3797419 Jul 18 '14 at 15:40
  • when using windows, you have to enclose the print with parentheses... like print("blablabla") .... on the other hand, I believe your error has something to do with tabs ... please follow this link, download the file and try to run it without modifying it... use slow download if you don't have premium account with that server... http://www.filefactory.com/file/7c6p31lpvshf/studentScore.py – mlwn Jul 18 '14 at 15:47
  • That downloaded and ran. Many Thanks! You think the error has something to do with tabs. I was told to use the untabify region the first time that I opened the code from the .txt. Could that be the issue? – user3797419 Jul 18 '14 at 15:57
  • tabs are very important in python... unlike java or C/C++, beginning and ending of procedures, loops... etc.. is defined with the tab order of each line.. there is no brackets to show the compiler the beginning and ending... that must be what went wrong with you... on the other side, there are slight differences when you run python on windows.. myself, I am running it on linux machine.. keep on reading from python's website (www.python.org) ... happy coding.. :) .. I hope the problem is solved now !! – mlwn Jul 18 '14 at 16:00
  • you could mark my answer as solution :) it would increase my reputation – mlwn Jul 18 '14 at 16:03
  • Answer is marked. I tried to up vote but I don't have the reputation points to do that. – user3797419 Jul 19 '14 at 07:31
  • Nevermind.. Whenever you need help, you can contact me quickly on my email address... michel.alwan@yandex.com – mlwn Jul 19 '14 at 08:02