기본적으로 내 코드는 파일이없는 상태에서 실행될 때 텍스트 파일을 만들지 만, 이전 파일에서 작동하는 코드를 복사하더라도 쓰기를하지 않는다. 내가 잘못했는지 전혀 모른다는 이유로 모든 코드를 복사하고 있습니다.파이썬이 파일에 쓰지 않는다.
#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
안녕하세요, 코드는 길고 잘못 작성되었습니다 (예 : 변수 이름 지정). 우리가 그 모든 것을 통과 할 것으로 기대하지 마십시오. 인쇄물을 몇 장 찍고 그것을 디버그하고 실패한 정확한 위치를 찾으십시오. 그럼 우리 한테 돌아와. 실제로 문제를 직접 해결할 수도 있습니다. – freakish
문제를 재현 할 수있는 최소한의 예제를 제공해야합니다. 이 끔찍한 스크립트는 이해하기 쉽지 않고 디버깅하기도 쉽지 않습니다. 작고 지정된 함수로 작업해야합니다. 그러면 프로그램의 거의 모든면이 개선 될 것입니다. – tamasgal
난 겨우 10 개만 너무 가볍게 굴지 말아주세요. –