在Python上读写

时间:2014-11-24 05:20:16

标签: python

目前我正在做这个由以下几点做的小事:

  1. 输入必须是键盘上的交互式。您将为12名学生提供意见。
  2. 您将输入学生的姓名和平均成绩。学生不能进入 平均值低于零或高于100.您的程序必须引发并处理异常 如果发生这种情况
  3. 将所有输出写入名为grades.txt
  4. 的文件
  5. 关闭输出文件。
  6. 打开文件grades.txt进行输入。
  7. 如果找不到文件,您的程序将引发并处理异常。
  8. 从文件中读取所有记录并显示它们。
  9. 我有这个到目前为止,但我不能使它工作。我该怎么办?

    outfile = open('grades.txt', 'w')
    
    
    def Main():
        print('Please make the Student List: ')
        Input()
         def Input():
    #STUDENT INPUT +++++++++++++++++++++++++
        print()
        Name1 = input("Enter your First Name: ")
        LName1 = input("Enter your Last Name: ")
        Grade1 = input("Enter your Grades: ")
        try:
             if Grade1 > 100:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
             elif Grade1 <= 0:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
        except:
            outfile.write(str(LName1) + ',' + str(Name1)+ ': ' + str(Grade1) + '\n')   
    
    # ++++++++++++++++++++++++++++++++       
        print()
        Name2 = input("Enter your First Name: ")
        LName2 = input("Enter your Last Name: ")
        Grade2 = input("Enter your Grades: ")
        try:
             if Grade2 > 100:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
             elif Grade2 <= 0:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
        except:
            outfile.write(str(LName2) + ',' + str(Name2)+ ': ' + str(Grade2) + '\n')   
    
    # +++++++++++++++++++++
    
        print()
        Name3 = input("Enter your First Name: ")
        LName3 = input("Enter your Last Name: ")
        Grade3 = input("Enter your Grades: ")
        try:
             if Grade3 > 100:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
             elif Grade3 <= 0:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
        except:
            outfile.write(str(LName3) + ',' + str(Name3)+ ': ' + str(Grade3) + '\n')   
    
    # +++++++++++++++++
        print()
        Name4 = input("Enter your First Name: ")
        LName4 = input("Enter your Last Name: ")
        Grade4 = input("Enter your Grades: ")
        try:
             if Grade4 > 100:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
             elif Grade4 <= 0:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
        except:
            outfile.write(str(LName4) + ',' + str(Name4)+ ': ' + str(Grade4) + '\n')   
    
    # +++++++++++++++++
        print()
        Name5 = input("Enter your First Name: ")
        LName5 = input("Enter your Last Name: ")
        Grade5 = input("Enter your Grades: ")
        try:
             if Grade5 > 100:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
             elif Grade5 <= 0:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
        except:
            outfile.write(str(LName5) + ',' + str(Name5)+ ': ' + str(Grade5) + '\n')   
    
    # ++++++++++++++++++++++++
        print()
        Name6 = input("Enter your First Name: ")
        LName6 = input("Enter your Last Name: ")
        Grade6 = input("Enter your Grades: ")
        try:
             if Grade6 > 100:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
             elif Grade6 <= 0:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
        except:
            outfile.write(str(LName6) + ',' + str(Name6)+ ': ' + str(Grade6) + '\n')   
    
    # ++++++++++++++++++++
    
        print()
        Name7 = input("Enter your First Name: ")
        LName7 = input("Enter your Last Name: ")
        Grade7 = input("Enter your Grades: ")
        try:
             if Grade7 > 100:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
             elif Grade7 <= 0:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
        except:
            outfile.write(str(LName7) + ',' + str(Name7)+ ': ' + str(Grade7) + '\n')   
    
    # ++++++++++++++++++++
    
        print()
        Name8 = input("Enter your First Name: ")
        LName8 = input("Enter your Last Name: ")
        Grade8 = input("Enter your Grades: ")
        try:
             if Grade8 > 100:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
             elif Grade8 <= 0:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
        except:
            outfile.write(str(LName8) + ',' + str(Name8)+ ': ' + str(Grade8) + '\n')   
    
    # ++++++++++++++++++++
    
        print()
        Name9 = input("Enter your First Name: ")
        LName9 = input("Enter your Last Name: ")
        Grade9 = input("Enter your Grades: ")
        try:
             if Grade9 > 100:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
             elif Grade9 <= 0:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
        except:
            outfile.write(str(LName9) + ',' + str(Name9)+ ': ' + str(Grade9) + '\n')   
    
    # ++++++++++++++++++
        print()
        Name10 = input("Enter your First Name: ")
        LName10 = input("Enter your Last Name: ")
        Grade10 = input("Enter your Grades: ")
        try:
             if Grade10 > 100:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
             elif Grade10 <= 0:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
        except:
            outfile.write(str(LName10) + ',' + str(Name10)+ ': ' + str(Grade10) + '\n')   
    
    # ++++++++++++++++++++++++
        print()
        Name11 = input("Enter your First Name: ")
        LName11 = input("Enter your Last Name: ")
        Grade11 = input("Enter your Grades: ")
        try:
             if Grade11 > 100:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
             elif Grade11 <= 0:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
        except:
            outfile.write(str(LName11) + ',' + str(Name11)+ ': ' + str(Grade11) + '\n')   
    
    # +++++++++++++++++++++
        print()
        Name12 = input("Enter your First Name: ")
        LName12 = input("Enter your Last Name: ")
        Grade12 = input("Enter your Grades: ")
        try:
             if Grade12 > 100:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
             elif Grade12 <= 0:
                 print ("Oops!  That was not valid answer.")
                 Input()
    
        except:
            outfile.write(str(LName12) + ',' + str(Name12)+ ': ' + str(Grade12) + '\n')   
    
    # +++++++++++++       
    
        outfile.close()
        Open()
         def Open():
        try:
            infile = open("grades.txt", "w")
    
        except: print("Error: can't find file or read data")
    
        line1 = infile.readline()
        line2 = infile.readline()
        line3 = infile.readline()
        line4 = infile.readline()
        line5 = infile.readline()
        line6 = infile.readline()
        line7 = infile.readline()
        line8 = infile.readline()
        line9 = infile.readline()
        line10 = infile.readline()
        line11 = infile.readline()
        line12 = infile.readline()
    
        infile.close()
    
        print(line1)
        print(line2)
        print(line3)
        print(line4)
        print(line5)
        print(line6)
        print(line7)
        print(line8)
        print(line9)
        print(line10)
        print(line11)
        print(line12)
    
    
    
    Main()
    

3 个答案:

答案 0 :(得分:1)

这是最简单的方法:

1)使用循环从用户获取所有值并将其存储在数组中。

 for i in range(12):
     #get details here

2)打开文件并写入数组内容。

 f = open("grades.txt", 'wt')
 #write list contents to file

3)打开相同的文件并显示内容。

 f = open("grades.txt", 'rt')
 #display file contents here

答案 1 :(得分:0)

Open()
 def Open():
try:
    infile = open("grades.txt", "r")

except: print("Error: can't find file or read data")

当您打开文件进行阅读时,模式需要r。否则它会创建一个清除旧数据的新文件。同时将try块中的input分配给grade。< / p>

答案 2 :(得分:0)

您应该在循环中设置诸如数组或列表之类的结构来处理变量。 e.g。

names = ["" for x in range(12)]

for i in range(0,len(names) - 1):
   names[i] = input('Enter Name: ')

等等,在循环中分配每个变量。