0

This is my assignment:

1

I am currently on part 2a (Print all the players that played more than 1700 minutes).

This is my code so far:

def part1():

    createfile=open("Assignment4.txt", "a+")
    createfile.write(f"Player Name          MinsPlayed  Goals   Assists YellowCard\n")
    createfile.write(f"Lionel Messi     1943        19      4       4\n")
    createfile.write(f"Robert Lewandowski   1864        28      6       2\n")
    createfile.write(f"Harry Kane           2017        14      11      1\n")
    createfile.write(f"Jack Grealish        1977        6       10      6\n")
    createfile.write(f"Cristiano Ronaldo    1722        19      3       1\n")
    createfile.write(f"Zlatan Ibrahimovic   1102        14      1       2\n")
    createfile.write(f"Gerard Moreno        1735        14      2       3\n")
    createfile.write(f"Romelu Lukaku        1774        18      6       4\n")
    createfile.write(f"Kylian Mbappe        1706        18      6       3\n")
    createfile.write(f"Erlin Haaland        1542        17      4       2")
    createfile.close()

part1()

def part2():

    filetoopen=open("Assignment4.txt", "r")
    for line in filetoopen.readlines():    
        linetosplit=line.split(' ')
        players = []
        player_name = (linetosplit[0] + ' ' + linetosplit[1])
        mins_played = (linetosplit[2])
        goals = (linetosplit[3])
        assists = (linetosplit[4])
        yellow_card = (linetosplit[5])
        players.append([player_name, mins_played, goals, assists, yellow_card]) 
    filetoopen.close()
            
    if mins_played > 1700:
        print(player_name)

part2()

When I run it this error message pops up

TypeError: '>' not supported between instances of 'str' and 'int'

I then tried fixing it by changing

mins_played = (linetosplit[2])

to

mins_played = int(linetosplit[2])

but then this error message popped up

ValueError: invalid literal for int() with base 10: ''

Reti43
  • 9,656
  • 3
  • 28
  • 44
ATSpiro
  • 11
  • this error means `linetosplit[2]`'s value is `''` which cannot be converted into an int – Macattack Mar 06 '21 at 00:18
  • Have you tried printing the contents of `linetosplit` to see if the output is what you expect it to be? – G. Anderson Mar 06 '21 at 00:20
  • You should use tab (“\t”) as delimiter between columns and then split input lines by tab line.split(“\t”). Then the value in minutes will be correctly parsed to int. – emirc Mar 06 '21 at 00:21
  • Does this answer your question? [Is there a difference between .split(" ") vs .split()](https://stackoverflow.com/questions/62013468/is-there-a-difference-between-split-vs-split) – Reti43 Mar 06 '21 at 00:49

1 Answers1

0

Check what linetosplit actually returns. In this case you will see that it returns

['Player', 'Name', '', '', '', '', '', '', '', '', '', 'MinsPlayed', '', 'Goals', '', '', 'Assists', 'YellowCard\n']

['Lionel', 'Messi', '', '', '', '', '1943', '', '', '', '', '', '', '', '19', '', '', '', '', '', '4', '', '', '', '', '', '', '4\n']

['Robert', 'Lewandowski', '', '', '1864', '', '', '', '', '', '', '', '28', '', '', '', '', '', '6', '', '', '', '', '', '', '2\n']
...

As all the spaces have been split. As mentioned by @Reti43 there is a difference between line.split(' ') and line.split().

However, in your case that does not entirely solve your issue. As the first line of the file is the definition of what each column is. And you cannot convert this string to an integer.

One way around this is to not include the first line when looping. There are multiple different ways of doing this, but I did it using list manipulation by excluding the first item of the list.

for line in filetoopen.readlines()[1:]:
    # Code

This excludes the first line.

bobveringa
  • 230
  • 2
  • 12
  • Okay so I now have this but it returns nothing: filetoopen=open("Assignment4.txt", "r") for line in filetoopen.readlines()[1:]: linetosplit=line.split() players = [] player_name = (linetosplit[0] + ' ' + linetosplit[1]) mins_played = int (linetosplit[2]) goals = int (linetosplit[3]) assists = int (linetosplit[4]) yellow_card = int (linetosplit[5]) players.append([player_name, mins_played, goals, assists, yellow_card]) filetoopen.close() if mins_played > 1700: print(player_name) – ATSpiro Mar 06 '21 at 01:24
  • @ATSpiro Do not move the goalpost of the question. You asked about an error. Your code is flawed and by fixing that, you encounter the next one, but this is not the place to deal with that new error. Hint: Read your assignment carefully, you're supposed to iterate every item in your list and check for that condition. – Reti43 Mar 06 '21 at 01:31