Python報錯TypeError: '<' not supported between instances of 'str' and 'int'


1 n = input()
2 if n>=100:print(int(n)/10)
3 else:print(int(n)*10)

報錯內容:

 

Traceback (most recent call last):
  File "1.py", line 12, in <module>
    if n>=100:print(int(n)/10)
TypeError: '>=' not supported between instances of 'str' and 'int'

***Repl Closed***

 

 

分析:input()返回的數據類型是str,不能直接和整數進行比較,必須先把str換成整數,使用int()方法

          因此,將input變量轉換為int型即可。

1 n = input()
2 n = int(n)
3 if n>=100:print(int(n)/10)
4 else:print(int(n)*10)

 或者

1 n = int(input("Input a number:"))
2 if n>=100:print(int(n)/10)
3 else:print(int(n)*10)

 

 

          

 


免責聲明!

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



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