【问题标题】:Please could someone advise on code format请有人就代码格式提出建议
【发布时间】:2020-02-07 21:53:32
【问题描述】:

我最初必须编写代码将 3 行写入一个名为 output.txt 的外部文本文件。第一行显示第一行的最小数量,第二行将显示第二行的最大数量,第三行将显示第三行的平均值。无论输入什么数字或长度,它仍然会显示所有最小值、最大值和平均值。 问题是代码仅将最后一个平均行写入输出文本文件。

我的讲师希望格式保持不变,但他有这些 cmets:

minmax 行未写入输出文件。 这是因为您没有将值写入 report_line 变量,该变量存储要写入输出文件的字符串。

尝试在 for 循环开始之前将 report_line 初始化为空字符串。

然后,您可以使用 += 运算符和换行符将输出存储在每次重复 for 循环时的 report_line 变量中。

我尝试了它们,但仍然得到相同的结果。仅打印平均线。

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.

【问题讨论】:

  • 请出示input.txt内容

标签: python for-loop external-data-source


【解决方案1】:

根据您的讲师的评论,确保将所有 report_line 更改更改为 +=。使用 = 覆盖现有值,丢弃任何以前的输出。特别是这一行:

report_line += "The {} of {} is {}.\n".format(operator, newData, result)
#           ^

您需要为 minmax 情况添加额外的 report_line += ... 语句。您正在计算这些值,但没有将它们添加到 report_line

或者取消缩进上面的report_line += ... 行,这样它就不会嵌套在 avg 大小写中。如果您取消缩进,那么它将适用于所有三种情况。

此外,使用\n 换行。将report_line+='n' 更改为:

report_line+='\n'

【讨论】:

    【解决方案2】:

    您正在覆盖第 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()
    

    【讨论】:

    • 运行代码时出现错误“未定义结果”
    • 代码适用于您提供的 input.txt。如果您有空行,或者有不同运算符的行,则您的 operator == "something" 检查未通过,结果未定义
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多