input()报错:TypeError: '>=' not supported between instances of 'str' and 'int'

    今天学习python基础—分支与循环,接触到了if。在练习一个if语句的时候,出现了错误。

    题目是: 根据分数划分成四个级别:优秀、良好、及格、不及格,分数是72:

grade = 72
if grade >= 90:
print('优秀')
elif grade >=70:
print('良好')
elif grade >=60:
print('及格')
else:
print('不及格')
 
这种情况下没有报错,打印出:良好。
    然后我就想换一种方法,把前几天学到的input也用进去,根据输入的成绩来判断分数属于哪个级别,代码如下:
grade = input('请输入你的分数:')
if grade >= 90:
print('优秀')
elif grade >=70:
print('良好')
elif grade >=60:
print('及格')
else:
print('不及格')
当我输入分数为85时,报错:TypeError: '>=' not supported between instances of 'str' and 'int'

报错原因是:input()输入的内容是一个字符串,字符串跟整型数值进行比较,类型不匹配

修改方法:

grade = int(input('请输入你的分数:'))

 

posted @ 2017-08-14 16:32  可爱小米兜  阅读(19529)  评论(2编辑  收藏  举报