2017-10-15 28 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 
相关问题