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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM