循环文件输入/输出中的测试分数

时间:2014-07-09 01:04:27

标签: python loops file-io

对于我的编程作业,我必须

  

使用文件输入和输出创建2个程序。第一个方案   提示用户输入5个测试分数,然后将其写入自己的分数   在名为tests.txt的文件中的行。然后第二个程序读取   tests.txt,显示所有分数和分数的平均值。

所以我提交了以下代码并获得了一个好的评分,并提供以下反馈:

  

我扣除了设计不佳的要点。当您发现代码重复时   几乎相似的线条,使用循环。你需要在两个程序中都有一个循环。

所以,我坚持清理设计并在其中放置一个循环(尽管我认为这是我的For Loop正在做的事情)。非常感谢帮助和澄清= D

这是第一个程序:

def main():
    score1=int(input("Please input your first score: "))
    score2=int(input("Please input your second score: "))
    score3=int(input("Please input your third score: "))
    score4=int(input("Please input your fourth score: "))
    score5=int(input("Please input your fifth score: "))

    score_file=open("tests.txt","w")

    for count in range(1, 5):
        score_file.write(str(score1) + "\n")
        score_file.write(str(score2) + "\n")
        score_file.write(str(score3) + "\n")
        score_file.write(str(score4) + "\n")
        score_file.write(str(score5) + "\n")

    score_file.close()

main()

这是第二个程序:

def main():

    display_score=open("tests.txt", "r")
    for count in range(1,5):  
        score1=int(display_score.readline())
        score2=int(display_score.readline())
        score3=int(display_score.readline())
        score4=int(display_score.readline())
        score5=int(display_score.readline())

    display_score.close()

    average=(score1+score2+score3+score4+score5)/5

    print("Here are your scores you inputted: ", \
      score1, ", ",score2,", ",score3,", ",score4,", ",score5,\
      sep="")
    print("Your average is: ", average, "%",\
      sep="")

main()

2 个答案:

答案 0 :(得分:1)

阅读分数:

all_scores = []

for x in range(5):
   all_score.append( int(input("score: ")) )

print all_scores

与写入文件的方式相同

for one_score in all_scores:
    score_file.write( str(one_score) + "\n")

但你可以在一个循环中完成:

for x in range(5):
    score = input("score: ")
    score_file.write( score + "\n")
顺便说一句:在第一个程序中,您不需要将分数转换为int()然后转换为str()

您可以使用for添加分数(或使用特殊功能sum(all_scores)

sum_score = 0

for one_score in all_scores:
    sum_score = sum_score + one_score

print( sum_score / len(all_scores) )

len(all_scores)会在列表中为您提供分数

当然你可以使用for循环来读取文件中的数字

all_scores = []

for count in range(5):  
    all_scores.append( int(display_score.readline()) )

print( all_scores )

从列表中打印元素有特殊功能

print( ", ".join(all_scores) )

所有值都将通过", "

连接

答案 1 :(得分:1)

评论:

  • 您的程序实际上都是错误的 - 您的for循环读取5个值,5次(总共25个得分条目)

  • 每当您发现编号变量(score1score2等)时,通常应该使用列表

  • with是处理文件的更好方法;它始终确保它们正确关闭

  • str.format()(新式字符串格式化)通常比字符串连接更快更容易阅读

以下是第一个程序的简化版

def main():
    with open("tests.txt", "w") as score_file:
        for tag in ["first", "second", "third", "fourth", "fifth"]:
            prompt = "Please input your {} score: ".format(tag)
            score  = int(input(prompt))
            output = "{}\n".format(score)
            score_file.write(output)

if __name__=="__main__":
    main()

一样运行
Please input your first score: 1
Please input your second score: 2
Please input your third score: 3
Please input your fourth score: 4
Please input your fifth score: 5

和第二个:

def average(lst):
    return float(sum(lst)) / len(lst)    

def main():
    with open("tests.txt") as score_file:
        scores = [int(line) for line in score_file]

    print("Here are your scores you inputted:")
    print(", ".join(str(score) for score in scores))

    print("Your average is: {}".format(average(scores)))

if __name__=="__main__":
    main()

一样运行
Here are your scores you inputted:
1, 2, 3, 4, 5
Your average is: 3.0