2014-11-19 4 views
-5

기본적으로 내 코드는 파일이없는 상태에서 실행될 때 텍스트 파일을 만들지 만, 이전 파일에서 작동하는 코드를 복사하더라도 쓰기를하지 않는다. 내가 잘못했는지 전혀 모른다는 이유로 모든 코드를 복사하고 있습니다.파이썬이 파일에 쓰지 않는다.

#Needed to generate random numbers 
import random 
pw = 0 
z = 0 
#this is setiing which test it is 
tn = 1 
#This is setting the final scores for the 3 different tests 
fs1 = 0 
fs2 =0 
fs3 = 0 
fsa = 0 
name = input("What is your name?") 
if name != "1":  
    while z < 1: 
     try: 
      #So we can save to a certain file 
      cq = "What class are you in?\n\ 
       1.Class 1\n\ 
       2.Class 2\n\ 
       3.Class 3\n" 
      c = int(input(cq)) 
     except ValueError: 
      print ("Please enter a valid class number") 
      continue 
     else: 
      while tn < 4: 
       #This is so they hopefully do not break the program 
       print ("Please do not spell out the number") 
       #Create the total score 
       ts = 0 
       #Create the total questions 
       tq = 1 
       if tn == 1: 
        print ("This is the test for the 1st time") 
       elif tn == 2: 
        print ("This is the test for the 2nd time") 
       else: 
        print ("This is the test for the 3rd and final time") 
       while tq != 11: 
        #keep asking the question until input is number, enables trying 
        x = 0 
        #decides if the question is addition,subtraction or multiplication. 
        operation = random.randint(0,2) 
        #This will be the first nuber in the question. 
        numb = random.randint(1,12) 
        #The will be the second number in the question. 
        numc = random.randint(1,12) 
        #This will decide what operation you get 
        if operation == 0: 
         #This will print the question 
         print("This is question",tq) 
         print(numb,"+",numc) 
         while x < 1: 
          #This will calculate the correct answer 
          a = numb + numc 
          try: 
           #This is taking your input 
           sa = int(input("What is the answer?")) 
          except ValueError: 
           print("That is not a number, please enter again") 
           continue 
          else: 
           #This is checking your answer against the correct one 
           if sa == a: 
            print ("well done") 
            #this is adding 1 to your final score 
            ts = ts + 1 
           else: 
            #This will tell you the correct answer 
            print ("The correct answer was",a) 
           #This keeps count of the amount of questions you have had 
           x = x + 1 
           tq = tq + 1 
        #This will decide what operation you get 
        elif operation == 1: 
         #This will print the question 
         print("This is question",tq) 
         print(numb,"-",numc) 
         while x < 1: 
          #This will calculate the correct answer 
          a = numb - numc 
          try: 
           #This will take your input 
           sa = int(input("What is the answer?")) 
          except ValueError: 
           print ("That is not a number, please enter again") 
           continue 
          else: 
           #This is checking your answer against the correct one 
           if sa == a: 
            print ("well done") 
            #this is adding 1 to your final score 
            ts = ts + 1 
           else: 
            #This will tell you the correct answer 
            print ("The correct answer was",a) 
           #This keeps count of the amount of questions you have had 
           tq = tq +1 
           x = x + 1 
        #This will decide what operation you get 
        else: 
         #This will print the question 
         print("This is question",tq) 
         print(numb,"x",numc) 
         while x < 1: 
          #This will calculate the correct answer 
          a = numb * numc 
          try: 
           #This will take your input 
           sa = int(input("What is the answer?")) 
          except ValueError: 
           print("That is not a number, please enter again") 
           continue 
          else: 
           #This is checking your answer against the correct one 
           if sa == a: 
            print ("well done") 
            #this is adding 1 to your final score 
            ts = ts + 1 
           else: 
            #This will tell you the correct answer 
            print ("The correct answer was",a) 
           #This keeps count of the amount of questions you have had 
           tq = tq +1 
           x = x + 1 
       #This is setting the 3 scores that will be written 
       if tn == 1: 
        fs1 = ts 
       elif tn == 2: 
        fs2 == ts 
       else: 
        fs3 == ts 
       tn = tn + 1 
       z = z + 1 
    name = str(name) 
    fsa = fs1 + fs2 + fs3 
    fsa = int(fsa/3) 
    #checks which class you were in 
    if c == 1: 
     f = open("Class 1 r.txt","a") 
     if fs1 == 10: 
      fs1 = str(fs1) 
      f.writelines(fs1) 
      f.writelines(" ")  
     else: 
      fs1 = str(fs1) 
      f.writelines("0") 
      f.writelines(fs1) 
      f.writelines(" ") 
     if fs2 == 10: 
      fs2 = str(fs2) 
      f.writelines(fs2) 
      f.writelines(" ") 
     else: 
      fs2 = str(fs2) 
      f.writelines("0") 
      f.writelines(fs2) 
      f.writelines(" ") 
     if fs3 == 10: 
      fs3 = str(fs3) 
      f.writelines(fs3) 
      f.writelines(" ") 
     else: 
      fs3 = str(fs3) 
      f.writelines("0") 
      f.writelines(fs3) 
      f.writelines(" ") 
     f.writelines(name) 
     f.writelines("\n") 
     f = open("Class 1 A.txt","a") 
     f.writelines("\n") 
     f.writelines(name) 
     f.writelines(" ") 
     if fs1 == 10: 
      fs1 = str(fs1) 
      f.writelines(fs1) 
      f.writelines(" ")  
     else: 
      fs1 = str(fs1) 
      f.writelines("0") 
      f.writelines(fs1) 
      f.writelines(" ") 
     if fs2 == 10: 
      fs2 = str(fs2) 
      f.writelines(fs2) 
      f.writelines(" ") 
     else: 
      fs2 = str(fs2) 
      f.writelines("0") 
      f.writelines(fs2) 
      f.writelines(" ") 
     if fs3 == 10: 
      fs3 = str(fs3) 
      f.writelines(fs3) 
      f.writelines(" ") 
     else: 
      fs3 = str(fs3) 
      f.writelines("0") 
      f.writelines(fs3) 
      f.writelines(" ") 
     f = open("Class 1 AV.txt","a") 
     if fsa == 10: 
      fsa = str(fsa) 
      f.writelines(fsa) 
      f.writelines(" ") 
      f.writelines(name) 
      f.writelines("\n") 
     else: 
      fsa = str(fsa) 
      f.writelines("0") 
      f.writelines(fsa) 
      f.writelines(" ") 
      f.writelines(name) 
      f.writelines("\n") 




    #checks which class you were in 
    if c == 2: 
     f = open("Class 2 r.txt","a") 
     if fs1 == 10: 
      fs1 = str(fs1) 
      f.writelines(fs1) 
      f.writelines(" ")  
     else: 
      fs1 = str(fs1) 
      f.writelines("0") 
      f.writelines(fs1) 
      f.writelines(" ") 

     if fs2 == 10: 
      fs2 = str(fs2) 
      f.writelines(fs2) 
      f.writelines(" ") 
     else: 
      fs2 = str(fs2) 
      f.writelines("0") 
      f.writelines(fs2) 
      f.writelines(" ") 

     if fs3 == 10: 
      fs3 = str(fs3) 
      f.writelines(fs3) 
      f.writelines(" ") 
     else: 
      fs3 = str(fs3) 
      f.writelines("0") 
      f.writelines(fs3) 
      f.writelines(" ") 
     f.writelines(name) 
     f.writelines("\n") 
     f = open("Class 2 A.txt","a") 
     if fsa == 10: 
      fsa = str(fsa) 
      f.writelines(name) 
      f.writelines(" ") 
      f.writelines(fsa) 
      f.writelines("\n") 
      f.close() 
     else: 
      fsa = str(fsa) 
      f.writelines(name) 
      f.writelines(" ") 
      f.writelines(fsa) 
      f.writelines("\n") 
      f.close() 
     f = open("Class 2 AV.txt","a") 
     if fsa == 10: 
      fsa = str(fsa) 
      f.writelines(fsa) 
      f.writelines(" ") 
      f.writelines(name) 
      f.writelines("\n") 
     else: 
      fsa = str(fsa) 
      f.writelines("0") 
      f.writelines(fsa) 
      f.writelines(" ") 
      f.writelines(name) 
      f.writelines("\n")  
    #checks which class you were in 
    if c == 3: 
     f = open("Class 3 r.txt","a") 
     if fs1 == 10: 
      fs1 = str(fs1) 
      f.writelines(fs1) 
      f.writelines(" ")  
     else: 
      fs1 = str(fs1) 
      f.writelines("0") 
      f.writelines(fs1) 
      f.writelines(" ") 

     if fs2 == 10: 
      fs2 = str(fs2) 
      f.writelines(fs2) 
      f.writelines(" ") 
     else: 
      fs2 = str(fs2) 
      f.writelines("0") 
      f.writelines(fs2) 
      f.writelines(" ") 

     if fs3 == 10: 
      fs3 = str(fs3) 
      f.writelines(fs3) 
      f.writelines(" ") 
     else: 
      fs3 = str(fs3) 
      f.writelines("0") 
      f.writelines(fs3) 
      f.writelines(" ") 
     f.writelines(name) 
     f.writelines("\n") 
     f = open("Class 3 A.txt","a") 
     if fsa == 10: 
      fsa = str(fsa) 
      f.writelines(name) 
      f.writelines(" ") 
      f.writelines(fsa) 
      f.writelines("\n") 
      f.close() 
     else: 
      fsa = str(fsa) 
      f.writelines(name) 
      f.writelines(" ") 
      f.writelines(fsa) 
      f.writelines("\n") 
      f.close() 
     f = open("Class 3 AV.txt","a") 
     if fsa == 10: 
      fsa = str(fsa) 
      f.writelines(fsa) 
      f.writelines(" ") 
      f.writelines(name) 
      f.writelines("\n") 
     else: 
      fsa = str(fsa) 
      f.writelines("0") 
      f.writelines(fsa) 
      f.writelines(" ") 
      f.writelines(name) 
      f.writelines("\n") 
    f.close 
    print("Congratulations",name,", you scored",ts,"out of 10!") 
if name == "1": 
    from operator import itemgetter, attrgetter 
    while pw < 1: 
     try: 
      password = input("What is the password?:") 
     except ValueError: 
      if password != "1": 
       print("That was not the password try again") 
       continue 
     pw = pw + 1 
     menut = "Which class do you want to look at?: \n\ 
       1.Class 1\n\ 
       2.Class 2\n\ 
       3.Class 3\n" 
     answert = int(input(menut)) 
     ############################################################# 
     if answert == 1: 
      e = 0 
      while e != 1: 
       menut1 = "What format do you want to open the file in?: \n\ 
          1.Standerd\n\ 
          2.Alphabetical\n\ 
          3.Highest to lowest\n\ 
          4.Average\n\ 
          5.Exit\n" 
       answert1 = int(input(menut1)) 
       if answert1 == 1: 
        f = open("Class 1 r.txt","r") 
        print(f.read()) 
       if answert1 == 2: 
        with open ('Class 1 A.txt', 'r') as f: 
         names = sorted (name.rstrip ("\n") for name in f) 
        print (names) 
       if answert1 == 3: 
        with open ('Class 1 AV.txt', 'r') as f: 
         scores = sorted ((scores.rstrip ("\n") for scores inf),reverse=True) 
        print (scores) 
       if answert1 == 4: 
        with open ('Class 1 AV.txt', 'r') as f: 
         scores = sorted ((scores.rstrip ("\n") for scores inf),reverse=True) 
        print (scores) 
       if answert1 == 5: 
        e = e + 1 
     ############################################################# 
     if answert == 2: 
      e = 0 
      while e != 1: 
       menut1 = "What format do you want to open the file in?: \n\ 
          1.Standerd\n\ 
          2.Alphabetical\n\ 
          3.Highest to lowest\n\ 
          4.Average\n\ 
          5.Exit\n" 
       answert1 = int(input(menut1)) 
       if answert1 == 1: 
        f = open("Class 2 A.txt","r") 
        print(f.read()) 
       if answert1 == 2: 
        with open ('Class 2 A.txt', 'r') as f: 
         names = sorted (name.rstrip ("\n") for name in f) 
        print (names) 
       if answert1 == 3: 
        with open ('Class 2 N.txt', 'r') as f: 
         scores = sorted ((scores.rstrip ("\n") for scores inf),reverse=True) 
        print (scores) 
       if answert1 == 4: 
        e = e + 1 
       if answert1 == 5: 
        e = e + 1 

     ########################################################### 
     if answert == 3: 
      e = 0 
      while e != 1: 
       menut1 = "What format do you want to open the file in?: \n\ 
          1.Standerd\n\ 
          2.Alphabetical\n\ 
          3.Highest to lowest\n\ 
          4.Average\n\ 
          5.Exit\n" 
       answert1 = int(input(menut1)) 
       if answert1 == 1: 
        f = open("Class 3 A.txt","r") 
        print(f.read()) 
       if answert1 == 2: 
        with open ('Class 3 A.txt', 'r') as f: 
         names = sorted (name.rstrip ("\n") for name in f) 
        print (names) 
       if answert1 == 3: 
        with open ('Class 3 N.txt', 'r') as f: 
         scores = sorted ((scores.rstrip ("\n") for scores inf),reverse=True) 
        print (scores) 
       if answert1 == 4: 
        e = e + 1 
       if answert1 == 5: 
        e = e + 1 
+5

안녕하세요, 코드는 길고 잘못 작성되었습니다 (예 : 변수 이름 지정). 우리가 그 모든 것을 통과 할 것으로 기대하지 마십시오. 인쇄물을 몇 장 찍고 그것을 디버그하고 실패한 정확한 위치를 찾으십시오. 그럼 우리 한테 돌아와. 실제로 문제를 직접 해결할 수도 있습니다. – freakish

+1

문제를 재현 할 수있는 최소한의 예제를 제공해야합니다. 이 끔찍한 스크립트는 이해하기 쉽지 않고 디버깅하기도 쉽지 않습니다. 작고 지정된 함수로 작업해야합니다. 그러면 프로그램의 거의 모든면이 개선 될 것입니다. – tamasgal

+0

난 겨우 10 개만 너무 가볍게 굴지 말아주세요. –

답변

1
  1. 하나의 큰 팁 : 는, 개폐 사용하는 포장과 함께 사용하지 마십시오

    with open(fname, 'a') as src: 
        src.write(...) 
    

    코드의이 덩어리가 당신을 위해 파일을 닫습니다, 당신 때문에 돈 그것을 할 필요가 없습니다. 대부분 긴 프로그램에서 파일 설명자를 추적하고 닫는 것은 어렵습니다. 마침내 블록으로 닫아야합니다.

  2. 공백으로 파일 이름을 쓰지 마십시오. 따라서 패턴이 나쁜 것처럼 보입니다. 'my_file.txt'와 같이 _을 사용하십시오.

  3. 코드를 메소드로 분할하면 코드를 읽기가 매우 어렵습니다.

응용 프로그램에 어려움이있는 경우 먼저 파일에 무언가를 쓰고 싶은 작은 스 니펫을 작성한 다음 응용 프로그램에서 해당 코드를 사용하십시오. 그러나 먼저 이름이 명확하고 정의 된 방법으로 나누십시오.

+0

죄송합니다. 댓글로 작성하는 것은 읽을 수 없습니다. – Beri

+1

파일에 쓰는 데 여전히 문제가있는 경우 스 니펫과 함께 스택 질문을 올리면 간단한 질문에 답할 수 있습니다 :) – Beri

관련 문제