ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.