我是一个新手学生,正在尝试编写一个程序,将原始分数列表转换为字母等级列表。我需要循环、文件打开/关闭、if elif else语句和2个函数作为赋值条件。在
我要打开进行测试的文件如下所示:
108
99
0
-1
以下是目前为止的计划:
^{pr2}$如果我尝试运行它,我会得到一个类型错误:
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程序本身似乎可以正常工作,所以我很困惑。提前谢谢你的帮助。在
默认情况下,Python将输入作为字符串,因此需要将其转换为int 您需要从函数中返回一些内容,否则convertscore(line)将始终返回null。下面将在Python2.7中使用。请检查一下
打开文件并读入值时,它们的类型(或类)为str,因此需要将它们转换为int或float来进行数学检查。在
相关问题 更多 >
编程相关推荐