0

I'm extremely new to python and I'm trying to learn. I have a text file containing this:

1 Lebron 30 5
1 Curry 29 8
1 Durant 20 4
2 Lebron 35 3
2 Curry 39 6
2 Durant 15 8
3 Lebron 25 6
3 Curry 30 5
3 Durant 21 5

I want to be able to input a name and get the first, third, fourth and a multiplication of the third and fourth column.

I know how to ask for the name, but I can't get further than that. I imagine something like a text split and a for loop.

This is what I have done so far

fileName = input("Enter the file name: ")
playerName = input("Enter the player name: ")

inputFile = open(fileName, 'r')
text = inputFile.read()

print("%4s % 8s % 10s % 12s" % \
        ("game", "Points", 
        "Assists", "PointsxAssists"))
words = len(text.split())
1
  • I got everything to work now, but does anyone know how I can summarize each column from my output? I get this as output for Durant Enter the player name: "Durant" First three games of the year for Durant game points assists p x a 1 20 4 80 2 15 8 120 3 21 5 105 21 The last number 21 is the last number in points. Does anyone know how I can add all three points columns together? Thanks! Commented Oct 9, 2018 at 19:12

1 Answer 1

1

You could try something like this (assuming there are only 3 whitespaces on each line in the file):

file_name = input("Enter the file name: ")
filter_name = input("Enter a name for filtering: ")
filter_name = filter_name.lower()

with open(file_name) as f:
    print('Showing names that contain "{}"'.format(filter_name))
    print('{:4s} {:10s} {:10s} {:10s}'.format(
        'game', 'points', 'assists', 'p x a'))

    for line in f:
        line = line.strip()
        if len(line) > 0:
            game, name, col3, col4 = line.split()
            name = name.lower()

            if filter_name in name:
                col3 = int(col3)   # this may raise ValueError if it is not a valid int
                col4 = int(col4)   # this may raise ValueError if it is not a valid int
                product = col3 * col4

                print('{:4s} {:10d} {:10d} {:10d}'.format(
                    game, col3, col4, product))
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! I tried this though and it returns the following error message: 12 'game', 'points', 'assists', 'p x a')) 13 ---> 14 with open(inputFile) as f: 15 for line in f: 16 line = line.strip() TypeError: coercing to Unicode: need string or buffer, file found
You probably used open() before the with open(... line. You do NOT need to call open before that line.
So I do not get an error message now, however don't get any information from the textfile. Enter the file name: "basket.txt" game points assists p x a I want to be able to have an input for a name and get only the 3 games that player has played. If that makes sense. Thank you again I really appreciate your help.
@Clueless added filtering to my answer. Is that what you need?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.