2017-10-09 20 views
0

私は比較的Pythonの新機能で、ファイルの入力と出力に取り組んでいます。これを使用して(Python)ファイルの入力と出力のエラーの取得

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 

1 3 
    1 1 
    1 0 
    20 30 

、ここでは「soccer_out.txt」に出力し、次の「soccer_in.txt」としてこれを取り、としている私のコードです:ここでは、入力ファイルがありますコード:

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) 

しかし、私はというエラーを取得しています
ファイル "C:\ユーザー"、12行目、process_season 勝利で= points_earned // 3: TypeError://: 'str'と 'int'のサポートされていないオペランドタイプ

ありがとうございました。

+1

ファイルから何かを読むと、そのファイルには 'str'という型があります。 'int(points_earned)// 3'をそこに入れれば、' points_earned'が整数であれば問題ありません。 – Unni

答えて

1

文字列を分割しようとしています。

process_season()には、season[0]season[1]を整数としてキャストできます。

process_season(output_file, season_number, int(season[0]), int(season[1])) 
+0

パーフェクト。ありがとうございました! –

関連する問題