2017-10-15 10 views
-2

저는 초보자이며 원시 점수 목록을 문자 성적 목록으로 변환하는 프로그램을 작성하려고합니다. 루프, 파일 열기/닫기, if-elif-else 문 및 내 과제 기준에 대한 2 개의 함수가 필요합니다.왜이 유형 오류가 발생합니까? TypeError : '> =' 'str'과 'int'인스턴스 간에는 지원되지 않습니다.

내가 테스트를 위해 개방하고있어 파일은 다음과 같습니다 : 여기

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' 

convertedcore 프로그램이 자체적으로 정상적으로 작동하는 것처럼 보이므로 혼란 스럽습니다. 당신의 도움에 미리 감사드립니다.

+0

문자열을 비교하고 있습니다. 'line' - 정수'101'에 대해서, print (convertedcore (line), file = outfile)에서'line'을'int (line)'로 바꾸는 것이 좋습니다. –

답변

0

파이썬은 문자열로 입력을 받아, 그래서 당신은 INT로 변환해야합니다 그리고 당신은 항상 null를 돌려줍니다 기능 또는 convertscore (선) 뭔가를 반환해야합니다. 아래는 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 또는 float 형식으로 변환해야 수학적 검사를 수행 할 수 있습니다. 기본적으로

>>> 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