Why am I getting this type error? TypeError: '>=' not supported between instances of 'str' and 'int'
Why am I getting this type error? TypeError: '>=' not supported between instances of 'str' and 'int'
我是一名新手学生,正在尝试编写一个程序,将原始分数列表转换为字母成绩列表。我需要循环、文件 open/close、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 程序本身似乎工作正常,所以我很困惑。预先感谢您的帮助。
当您打开文件并读取值时,它们是(或class)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
默认情况下Python将输入作为字符串,因此您需要将其转换为 int
并且您需要 return 函数中的某些内容,否则 convertscore(line) 将始终 return 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)
我是一名新手学生,正在尝试编写一个程序,将原始分数列表转换为字母成绩列表。我需要循环、文件 open/close、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 程序本身似乎工作正常,所以我很困惑。预先感谢您的帮助。
当您打开文件并读取值时,它们是(或class)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
默认情况下Python将输入作为字符串,因此您需要将其转换为 int 并且您需要 return 函数中的某些内容,否则 convertscore(line) 将始终 return 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)