2017-10-15 64 views
-2

的實例之間不支持我是新手學生,想寫原始分數的列表轉換爲字母等級名單的程序。我需要循環,文件打開/關閉,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程序似乎自行工作,所以我很困惑。預先感謝您的幫助。

+0

你要比較的字符串; '對一個整數'101' line'-,我建議改變'line'到'INT(線)''上打印(convertscore(線),文件= OUTFILE)' –

回答

0

默認的Python需要輸入字符串,所以你需要將它轉換爲INT 你需要從功能或convertscore(線)返回的東西將返回NULL始終。下面將在Python 2.7中工作。請檢查

def convertscore(score): 
    grade = "" 
    if score >=101: 
     grade = "Score is over 100%. Are you sure this is right? \n" 
     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: 
     grade = "Score cannot be less than zero." 
    else: 
     grade = "Unable to convert score." 

    return grade 


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 
    outfile.write(convertscore(int(line))) 
    outfile.write("\n") 

    #close both files 
infile.close() 
outfile.close() 

#print() 
print("Letter grades were saved to", outfileName) 
+0

@studenthelpmeplease:能否請您標記的答案'有用'並將我的評論標記爲答案? –

0

當你打開文件,並在值讀書,他們的類型(或類)STR的,所以你需要把它們轉換成int或漂浮做數學檢查。

>>> prompt = 'What...is the airspeed velocity of an unladen swallow?\n' 
>>> speed = input(prompt) 
What...is the airspeed velocity of an unladen swallow? 
17 
>>> type(speed) 
<class str> 
>>> speed = int(speed) 
17 
>>> int(speed) + 5 
22 
相關問題