0

I am relatively new to Python and working with inputting and outputting in files. Here is the input file:

    1 3
    1 1
    1 0
    20 30

and here is my code that takes this as "soccer_in.txt" and is suppose to output the following into "soccer_out.txt":

    Season: 1, Games Played: 1, Points earned: 3
    Possible Win-Tie-Loss Records
    -----------------------------
    1-0-0

    Season: 2, Games Played: 1, Points earned: 1
    Possible Win-Tie-Loss Records
    -----------------------------
    0-1-0

    Season: 3, Games Played: 1, Points earned: 0
    Possible Win-Tie-Loss Records
    -----------------------------
    0-0-1

    Season: 4, Games Played: 20, Points earned: 30
    Possible Win-Tie-Loss Records
    -----------------------------
    10-0-10
    9-3-8
    8-6-6
    7-9-4
    6-12-2
    5-15-0

using this code:

def process_season(output_file, season, games_played, points_earned):
    output_file.write("Season: " + str(season) + ", Games Played: " + str(games_played) +
          ", Points earned: " + str(points_earned))
    output_file.write("Possible Win-Tie-Loss Records")
    output_file.write("-----------------------------")
    wins = points_earned // 3
    ties = points_earned % 3
    losses = games_played - wins - ties
    while (wins >= 0) and (losses >= 0):
            output_file.write(str(wins) + "-" + str(ties) + "-" + str(losses))
            wins -= 1
            ties += 3
            losses -= 2
    output_file.write()

# --------------------------------------

def process_seasons(input_file, output_file):
    season_number = 0
    for season in input_file:
        season_number += 1
    process_season(output_file, season_number, season[0], season[1])

# --------------------------------------
f_in=open("soccer-in.txt", "r")
f_out=open("soccer-out.txt", "w+")
process_seasons(f_in, f_out)

But I'm getting an error that says
File "C:\Users", line 12, in process_season wins = points_earned // 3 TypeError: unsupported operand type(s) for //: 'str' and 'int'

Any help would be appreciated Thank you.

1
  • 1
    When you read something from a file, that will have a type str. Just put int(points_earned) // 3 there and you should be fine as long as points_earned is an integer. Commented Oct 9, 2017 at 1:25

1 Answer 1

1

You're trying to divide a string.

In process_season() you can try casting season[0] and season[1] as integers.

process_season(output_file, season_number, int(season[0]), int(season[1]))
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thank you!

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.