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)