首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(Python)输入和输出文件时出现错误

(Python)输入和输出文件时出现错误
EN

Stack Overflow用户
提问于 2017-10-09 09:21:56
回答 1查看 35关注 0票数 0

我是Python的新手,在文件中处理输入和输出。以下是输入文件:

代码语言:javascript
复制
    1 3
    1 1
    1 0
    20 30

下面是我的代码,它将其视为"soccer_in.txt“,并假设将以下内容输出到”soccer_out.txt“中:

代码语言:javascript
复制
    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

使用以下代码:

代码语言:javascript
复制
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:\Users",第12行,位于process_season wins = points_earned // 3 TypeError:不支持的//操作数类型:'str‘和'int’

任何帮助都将不胜感激谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-10-09 09:32:28

你在试着分割一根线。

process_season()中,您可以尝试将season[0]season[1]转换为整数。

代码语言:javascript
复制
process_season(output_file, season_number, int(season[0]), int(season[1]))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46637661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档