TypeError: unorderable types: str() = int()
1.问题描述
1 age=input('please enter your age') 2 if age >=18: 3 print('your age is',age) 4 print('adult') 5 elif age>=6: 6 print('teenager') 7 else: 8 print('kid')
今天在运行该代码的时候,python解释器报错,错误信息如下:
F:\python>python3 elif.py
please enter your age5
Traceback (most recent call last):
File "elif.py", line 2, in <module>
if age >=18:
TypeError: unorderable types: str() >= int()
解释一下错误信息:
1.Traceback指追踪,即python的解释器使用Traceback追踪异常,出现Traceback说明你的程序出错了;
2.接下来的一行表明文件名,位置;
3.再接下来显示出错语句;
4.最后一行显示错误的原因。
unorderable types: str() > int()指类型不匹配,即age变量与18的类型不匹配。可是,我输入的age是整数,为什么会出现这种情况呢?
原因是,在python3中,input函数返回的是字符串,即你输入字符串,他就输出字符串,你输入数字,他还是输出字符串,那么在执行接下来的操作时就可能出现类型不匹配的情况。
所以出现了字符串与数字比较的情况。
2.解决方法
既然input函数的返回值是字符串,我们不妨将字符串强制性转化成整型,这样就不会出现类型不匹配的情况了。
正确代码如下:
1 age=int(input('please enter your age')) 2 if age >=18: 3 print('your age is',age) 4 print('adult') 5 elif age>=6: 6 print('teenager') 7 else: 8 print('kid')
运行结果:
F:\python>python3 elif.py
please enter your age25
your age is 25
adult
转载于:https://www.cnblogs.com/X-knight/p/7160523.html
TypeError: unorderable types: str() = int()相关推荐
- TypeError: keys must be str, int, float, bool or None, not tuple,解决 python 中 json 保存不了字典键值为 元组 的问题
>>> a = {('0xFFFA', 11): 32, ('0x00F3', 1): 1, ('0xFFFA', 2): 42} {('0xFFFA', 1): 32, ('0x0 ...
- python报错TypeError: must be str, not int
python报错TypeError: must be str, not int 字符串拼接一个整型变量报错,代码如下 for i in range(1,586):res = 'test' + i 报错 ...
- python cannot concatenate_解决Python 异常TypeError: cannot concatenate 'str' and 'int' objects
解决Python 异常TypeError: cannot concatenate 'str' and 'int' objects TypeError: cannot concatenate 'str' ...
- python运行报错TypeError: cannot concatenate 'str' and 'int' objects
dict运行报错 d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 } for key in d: print key+":& ...
- Python --提示 TypeError: cannot concatenate str and int objects
clientsocket,addr = socketserver.accept() print("client ip:"+addr[0]) print("client p ...
- java indexof int,int indexOf(String str, int fromIndex)
int indexOf(String str, int fromIndex) 描述 (Description) java.lang.String.indexOf(String str, int fro ...
- 成功解决TypeError: object of type ‘int‘ has no len()
成功解决TypeError: object of type 'int' has no len() 目录 解决问题 解决思路 解决方法 解决问题 TypeError: object of type 'i ...
- python3 出现错误:TypeError: must be str, not list
def make_pizza(*toppings):for j in range(0,len(toppings)):print(str(j) + toppings[j]) list1 = ['s1', ...
- C语言九十四之请编写函数fun(char *str, int n),其功能是:使字符串str的前导*号不能多余n个,若多于n个,则删除多余的*号,若少于或等于n个,则不做处理。
1.题目 规定输入的字符串中只包含字母和*号,请编写函数fun(char *str, int n),其功能是:使字符串str的前导*号不能多余n个,若多于n个,则删除多余的*号,若少于或等于n个,则不 ...
最新文章
- 作业二:编写一个自动生成四则运算的程序
- 使用telnet模拟邮件的收发
- 【转载】SAP内部订单概念信息
- 计算机作文1500字,作文1500字
- php远程下载头像,远程使用gravatar头像
- 保护 .NET Core 项目的敏感信息
- 融创上海:从营销切入,建立自上而下的数据平台,实现数据应用
- Keras自定义Loss函数
- 靶形数独(信息学奥赛一本通-T1447)
- 【https】keystore was tampered with or password was incorrect
- 苹果发布 iOS 和 macOS 更新,修复已遭利用0day
- 10个必知的网页设计术语计算机与网络,入学测试计算机与网络应用基础知识题库(公开题)教学文稿(10页)-原创力文档...
- inline在C99以及Gcc中的处理方式[转]---很好的一篇总结
- win10无法装载iso文件_装载Win10 ISO镜像文件的具体方法
- 【事件相机整理】信号处理、噪声与滤波
- 2022年全球市场激光直接成像系统(LDI)总体规模、主要生产商、主要地区、产品和应用细分研究报告
- k8s健康检查(七)
- Android 折叠屏适配最全的攻略在这里
- 织梦dedecms 忘记管理员后台密码的解决技巧
- 几分钟内上线一个网站,这些神器我爱了!
热门文章
- x264编码指南——码率控制
- node代理请求 vue_vue-cli项目开发/生产环境代理实现跨域请求+webpack配置开发/生产环境的接口地址...
- php可以在dw中吗,php新手求助,为什么在DW中设计里能看到运行php程序的结果,但…...
- 语言 泰克示波器程序_示波器再升级,EMI测试不求人
- java.sql.SQLException: null,message server: Host ora-rac2 is blocked because of many
- liunx安装和部署nacos
- layer右下脚弹窗
- HDOJ水题集合8:DBFS
- 【CCCC】L2-020 功夫传人 (25分),,模拟水题,多叉树的存储与遍历
- 计算机博士复试英语自我介绍,博士生复试自我介绍的英文