的實例之間不支持我是新手學生,想寫原始分數的列表轉換爲字母等級名單的程序。我需要循環,文件打開/關閉,if-elif-else語句以及2個用於我的賦值條件的函數。爲什麼我得到這種類型的錯誤?類型錯誤:「> =」「海峽」和「廉政」
的文件,我打開測試看起來是這樣的:
108
99
0
-1
這裏的程序至今:
def convertscore(score):
grade = ""
if score >=101:
print("Score is over 100%. Are you sure this is right?")
grade = "A"
elif score >=90:
grade = "A"
elif score >=80 <=89:
grade = "B"
elif score >=70 <=79:
grade = "C"
elif score >= 60 <=69:
grade = "D"
elif score >=0 <=59:
grade = "F"
elif score < 0:
print("Score cannot be less than zero.")
else:
print("Unable to convert score.")
print(grade)
def main():
print("This program creates a file of letter grades from a file of scores on a 100-point scale.")
print()
#get the file names
infileName = input("What file are the raw scores in? ")
outfileName = input("What file should the letter grades go in? ")
#open the files
infile = open(infileName, 'r')
outfile = open(outfileName, 'w')
#process each line of the output file
for line in infile:
#write to output file
print(convertscore(line), file=outfile)
#close both files
infile.close()
outfile.close()
print()
print("Letter grades were saved to", outfileName)
main()
如果我嘗試運行它,我得到一個錯誤類型:
Traceback (most recent call last):
File "/Users/xxxx/Documents/convertscore.py", line 54, in <module>
main()
File "/Users/xxxx/Documents/convertscore.py", line 45, in main
print(convertscore(line), file=outfile)
File "/Users/xxxx/Documents/convertscore.py", line 10, in convertscore
if score >=101:
TypeError: '>=' not supported between instances of 'str' and 'int'
convertscore程序似乎自行工作,所以我很困惑。預先感謝您的幫助。
你要比較的字符串; '對一個整數'101' line'-,我建議改變'line'到'INT(線)''上打印(convertscore(線),文件= OUTFILE)' –