2017-10-09 2 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

완벽. 고맙습니다! –