Re: [Tutor] help with reading a string?

2018-07-16 Thread Steven D'Aprano
Hi Crystal, and welcome! My response is further below, after your 
question.

On Mon, Jul 16, 2018 at 05:28:37PM -0500, Crystal Frommert wrote:
> Hi, I am a beginner comp sci teacher at an all-girls high school. The girls
> are learning how to read from a txt file and search for a string.
> 
> Here is a sample of text from the txt file:
> TX,F,1910,Mary,895
> TX,F,1910,Ruby,314
> TX,F,1910,Annie,277
> TX,F,1910,Willie,260
> TX,F,1910,Ruth,252
> TX,F,1910,Gladys,240
> TX,F,1910,Maria,223
> TX,F,1910,Frances,197
> TX,F,1910,Margaret,194
> 
> How do they read the number after a certain searched name and then add up
> the numbers after each time the name occurs?

Which version of Python are you using? The answer may depend on the 
exact version, but something like this ought to be good:

# Code below is not tested, please forgive any bugs when you try it...

# Process the lines of some text file.
total = 0
with open("path to the file.txt", "r") as f:
# Read the file line-by-line.
for line in f:
# Split each record into five fields.
# Please pick more meaningful field names for a, b, c!
a, b, c, name, score = line.split(",")
# Choose only records for a specific person.
# Here I hard-code the person's name, in practice
# you ought to use a variable.
if name == "Sarah":
# Convert the score from a string to an int, 
# and add it to total.
total = total + int(score)
# Dropping back to the extreme left automatically closes
# the file.
print(total)


Remember that in Python, indentation is not optional.

Some extra features:

* You can re-write the line "total = total + int(score)" using the more
  compact notation "total += int(score)"

* Instead of the "with open(...) as f", you can do it like this:

f = open("path to the file.txt")  # the "r" parameter is optional
for line in f:
# copy for loop from above to here
# (don't forget to reduce the indentation by one level)
...
# Explicitly close the file. 
f.close()# A common error is to forget the parentheses!
# And finally print the total.
print(total)


Hope this helps, and please don't hesitate to ask if you have any 
questions. Keep your replies on the mailing list so others may 
contribute answers and learn from the responses.


Regards,



Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with reading a string?

2018-07-16 Thread Alan Gauld via Tutor
On 16/07/18 23:28, Crystal Frommert wrote:

> are learning how to read from a txt file and search for a string.
> 
> Here is a sample of text from the txt file:
> TX,F,1910,Mary,895
> TX,F,1910,Ruby,314
> TX,F,1910,Annie,277
> 
> How do they read the number after a certain searched name and then add up
> the numbers after each time the name occurs?
> 
> I would really like to help them with this code but I am at a loss.

Which bit are you at a loss over?

The general approach is:

open the file for reading
read each line.
search the line for your target string
extract the number at the end of the file
add the number to the running total

None of those are intrinsically difficult, so which
bit are you having problems with?

Have you got any code that you tried and it didn't work?
That would be most helpful in determining where you
need to adjust your approach.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] help with reading a string?

2018-07-16 Thread Crystal Frommert
Hi, I am a beginner comp sci teacher at an all-girls high school. The girls
are learning how to read from a txt file and search for a string.

Here is a sample of text from the txt file:
TX,F,1910,Mary,895
TX,F,1910,Ruby,314
TX,F,1910,Annie,277
TX,F,1910,Willie,260
TX,F,1910,Ruth,252
TX,F,1910,Gladys,240
TX,F,1910,Maria,223
TX,F,1910,Frances,197
TX,F,1910,Margaret,194

How do they read the number after a certain searched name and then add up
the numbers after each time the name occurs?

I would really like to help them with this code but I am at a loss.


Thank you, Crystal
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor