寫此刻我在做這個小excerscie是由做的:閱讀和Python的
- 輸入必須是從鍵盤互動。你將爲12名學生提供投入。
- 您將輸入學生的姓名和平均成績。學生無法輸入 平均值在零以下或100以上。如果發生這種情況,您的程序必須提出並處理例外 。
- 將所有輸出寫入名爲grades.txt的文件
- 關閉輸出文件。
- 打開文件grades.txt進行輸入。
- 如果未找到該文件,程序將引發並處理異常。
- 從文件中讀取所有記錄並顯示它們。
我有這個到目前爲止,但我不能讓它工作。我能做什麼?
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()
如果你不得不重複這麼多的代碼,你應該自己考慮是否可以使用循環。 – icktoofay 2014-11-24 05:23:36
'try'塊中的'Input()'沒有被賦值給任何變量。 – vks 2014-11-24 05:24:27
輸入設置爲Def Def Input()? – 2014-11-24 05:28:18