我是Python的新手,在文件中处理输入和输出。以下是输入文件:
1 3
1 1
1 0
20 30下面是我的代码,它将其视为"soccer_in.txt“,并假设将以下内容输出到”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使用以下代码:
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’
任何帮助都将不胜感激谢谢。
发布于 2017-10-09 09:32:28
你在试着分割一根线。
在process_season()中,您可以尝试将season[0]和season[1]转换为整数。
process_season(output_file, season_number, int(season[0]), int(season[1]))https://stackoverflow.com/questions/46637661
复制相似问题