今天學習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('請輸入你的分數:'))
