请有人就代码格式提出建议
Please could someone advise on code format
我最初不得不编写代码将 3 行写入名为 output.txt
的外部文本文件。第一行显示第一行的最小数,第二行显示第二行的最大数,第三行显示第三行的平均值。无论输入什么数字或长度,它仍然会显示最小值、最大值和平均值的所有值。
问题是代码只将最后一个平均行写入输出文本文件。
我的讲师希望格式保持不变,但他有以下评论:
The min
and max
lines are not being written to the output file.
This is because you do not write the values to the report_line
variable, which stores the string to write to the output file.
Try initializing report_line
to be an empty string before the for loops begin.
You can then use +=
operator and new line characters to store the output in the report_line
variable on each repetition of the for loop.
我试过了,但我仍然得到相同的结果。仅打印 avg 行。
outfile = open("output.txt", "w")
with open("input.txt") as f:
report_line = ""
for line in f:
operator, data = line.lower().strip().split(":")
line = line.split(":")
operator = line[0].lower().strip()
data = line[1].strip().split(",")
newData = []
for x in data:
report_line+='n'
newData.append(int(x))
if operator == "min":
result = min(newData)
elif operator == "max":
result = max(newData)
elif operator == "avg":
result = sum(newData) / len(newData)
report_line = "The {} of {} is {}.\n".format(operator, newData, result)
outfile.write(report_line)
outfile.close()
输入:
min:1,2,3,4,5,6
max:1,2,3,4,5,6
avg:1,2,3,4,5,6
输出应该是:
The min of [1, 2, 3, 5, 6] is 1.
The max of [1, 2, 3, 5, 6] is 6.
The avg of [1, 2, 3, 5, 6] is 3.4.
根据讲师的评论,确保将所有 report_line
更改更改为 +=
。使用 =
覆盖现有值,丢弃任何先前的输出。特别是这一行:
report_line += "The {} of {} is {}.\n".format(operator, newData, result)
# ^
您需要为 min 和 max 情况添加额外的 report_line += ...
语句。您正在计算这些值,但没有将它们添加到 report_line
。
或者取消缩进上面的 report_line += ...
行,这样它就不会嵌套在 avg 的情况下。如果您取消缩进,那么它将适用于所有三种情况。
此外,使用 \n
作为换行符。将 report_line+='n'
更改为:
report_line+='\n'
您正在覆盖第 20 行的 report_line。将 =
更改为 +=
而不是
report_line += "The {} of {} is {}.\n".format(operator, newData, result)
^
您还想缩进 for x in data
循环,因为您目前只使用最后一个运算符,即 avg。您想对数据中的每一行进行计算
您可能想删除这一行,因为您已经在 report_line 语句中添加了换行符
report_line+='n'
固定代码为
outfile = open("output.txt", "w")
with open("input.txt") as f:
report_line = ""
for line in f:
operator, data = line.lower().strip().split(":")
line = line.split(":")
operator = line[0].lower().strip()
data = line[1].strip().split(",")
newData = []
for x in data:
newData.append(int(x))
if operator == "min":
result = min(newData)
elif operator == "max":
result = max(newData)
elif operator == "avg":
result = sum(newData) / len(newData)
report_line += "The {} of {} is {}.\n".format(operator, newData, result)
outfile.write(report_line)
outfile.close()
我最初不得不编写代码将 3 行写入名为 output.txt
的外部文本文件。第一行显示第一行的最小数,第二行显示第二行的最大数,第三行显示第三行的平均值。无论输入什么数字或长度,它仍然会显示最小值、最大值和平均值的所有值。
问题是代码只将最后一个平均行写入输出文本文件。
我的讲师希望格式保持不变,但他有以下评论:
The
min
andmax
lines are not being written to the output file. This is because you do not write the values to thereport_line
variable, which stores the string to write to the output file.Try initializing
report_line
to be an empty string before the for loops begin.You can then use
+=
operator and new line characters to store the output in thereport_line
variable on each repetition of the for loop.
我试过了,但我仍然得到相同的结果。仅打印 avg 行。
outfile = open("output.txt", "w")
with open("input.txt") as f:
report_line = ""
for line in f:
operator, data = line.lower().strip().split(":")
line = line.split(":")
operator = line[0].lower().strip()
data = line[1].strip().split(",")
newData = []
for x in data:
report_line+='n'
newData.append(int(x))
if operator == "min":
result = min(newData)
elif operator == "max":
result = max(newData)
elif operator == "avg":
result = sum(newData) / len(newData)
report_line = "The {} of {} is {}.\n".format(operator, newData, result)
outfile.write(report_line)
outfile.close()
输入:
min:1,2,3,4,5,6
max:1,2,3,4,5,6
avg:1,2,3,4,5,6
输出应该是:
The min of [1, 2, 3, 5, 6] is 1.
The max of [1, 2, 3, 5, 6] is 6.
The avg of [1, 2, 3, 5, 6] is 3.4.
根据讲师的评论,确保将所有 report_line
更改更改为 +=
。使用 =
覆盖现有值,丢弃任何先前的输出。特别是这一行:
report_line += "The {} of {} is {}.\n".format(operator, newData, result)
# ^
您需要为 min 和 max 情况添加额外的 report_line += ...
语句。您正在计算这些值,但没有将它们添加到 report_line
。
或者取消缩进上面的 report_line += ...
行,这样它就不会嵌套在 avg 的情况下。如果您取消缩进,那么它将适用于所有三种情况。
此外,使用 \n
作为换行符。将 report_line+='n'
更改为:
report_line+='\n'
您正在覆盖第 20 行的 report_line。将 =
更改为 +=
而不是
report_line += "The {} of {} is {}.\n".format(operator, newData, result)
^
您还想缩进 for x in data
循环,因为您目前只使用最后一个运算符,即 avg。您想对数据中的每一行进行计算
您可能想删除这一行,因为您已经在 report_line 语句中添加了换行符
report_line+='n'
固定代码为
outfile = open("output.txt", "w")
with open("input.txt") as f:
report_line = ""
for line in f:
operator, data = line.lower().strip().split(":")
line = line.split(":")
operator = line[0].lower().strip()
data = line[1].strip().split(",")
newData = []
for x in data:
newData.append(int(x))
if operator == "min":
result = min(newData)
elif operator == "max":
result = max(newData)
elif operator == "avg":
result = sum(newData) / len(newData)
report_line += "The {} of {} is {}.\n".format(operator, newData, result)
outfile.write(report_line)
outfile.close()