LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-25-2010, 02:15 PM   #1
stryker213
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Rep: Reputation: 0
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.
 
Old 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: Reputation: 65
You can convert strings to integers in Python by passing the value to the int() function, like so:
Code:
>>> int('230498')
230498
>>>
 
Old 04-25-2010, 02:33 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
print int(L[0])

The int operator gives you the conversion you need.
 
Old 04-25-2010, 02:35 PM   #4
stryker213
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by indienick View Post
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.
 
Old 04-25-2010, 02:38 PM   #5
stryker213
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Mara View Post
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!
 
Old 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: Reputation: 65
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
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a .CSV file and then calculating average per minute basis in shell script. krishdeeps Linux - Newbie 1 04-23-2010 04:38 PM
Calculating Average in PHP Array? your_shadow03 Programming 1 01-27-2010 04:19 AM
Want to write a script for calculating usage of software from text file contains log sheraz Linux - Newbie 7 10-06-2009 03:05 AM
calculating average of a column of numbers in bash hta1984 Programming 4 12-13-2006 11:58 AM
Calculating age in days and month in a bash script jachba Programming 5 06-23-2006 01:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration