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)

 

 

          

 

posted @   BH8ANK  阅读(73464)  评论(1编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 手把手教你更优雅的享受 DeepSeek
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现
点击右上角即可分享
微信分享提示