Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Calculating average from Python Script
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 04-25-2010, 02:15 PM #1 stryker213 LQ Newbie   Registered: Dec 2008 Posts: 3 Rep: Calculating average from Python Script Hello, I'm trying to write a python script that will calculate my test scores from a text file named "exams." Sample input would be: Calculus 85 90 Physics 90 85 93 Output will look like: Calculus 87.5 Physics 89.3 I have the following code: Code: ```#!/usr/bin/python import math import sys f = open('exams',"r") l = f.readline() while l : l = l.split(None,10) L = l[1:] print sum print l[:1] print 'Scores ', L print 'Number of Values ', len(L) l = f.readline()``` My intent is to sum the values found in L and divide by len(L). I think my issue is that when I split the values, I get a string array (ex. ['85', '90'] ), which makes it hard to do calculations.
 04-25-2010, 02:29 PM #2 indienick Senior Member   Registered: Dec 2005 Location: London, ON, Canada Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD Posts: 1,853 Rep: You can convert strings to integers in Python by passing the value to the int() function, like so: Code: ```>>> int('230498') 230498 >>>```
 04-25-2010, 02:33 PM #3 Mara Moderator   Registered: Feb 2002 Location: Grenoble Distribution: Debian Posts: 9,642 Rep: print int(L[0]) The int operator gives you the conversion you need.
04-25-2010, 02:35 PM   #4
stryker213
LQ Newbie

Registered: Dec 2008
Posts: 3

Original Poster
Rep:
Quote:
 Originally Posted by indienick You can convert strings to integers in Python by passing the value to the int() function, like so: Code: ```>>> int('230498') 230498 >>>```
Thanks for the advice indienick. However, it's still not working. My code takes each line of text and parses it into a array so that it looks like this: ['Calculus', '85', '90']. Using L = l[1:], gives me ['85', '90'], which is close. If I used your method (L = int(l[1:])), I get an error.

04-25-2010, 02:38 PM   #5
stryker213
LQ Newbie

Registered: Dec 2008
Posts: 3

Original Poster
Rep:
Quote:
 Originally Posted by Mara print int(L[0]) The int operator gives you the conversion you need.
Mara, thank you very much for your help. Your method does work for individual iterations, but how can I make it so that it goes through my entire exam list? For example, I may have two test scores for one subject and four in another.

Thanks again!

 04-25-2010, 04:58 PM #6 indienick Senior Member   Registered: Dec 2005 Location: London, ON, Canada Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD Posts: 1,853 Rep: Maybe try something like this: Code: ```average = 0 for x in range(1, len(l)): score = int(l[x]) average += score average = float(average / (len(l) - 1)) print average```

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post krishdeeps Linux - Newbie 1 04-23-2010 04:38 PM your_shadow03 Programming 1 01-27-2010 04:19 AM sheraz Linux - Newbie 7 10-06-2009 03:05 AM hta1984 Programming 4 12-13-2006 11:58 AM jachba Programming 5 06-23-2006 01:37 PM

LinuxQuestions.org

All times are GMT -5. The time now is 03:12 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -