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()相关推荐

  1. 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 ...

  2. python报错TypeError: must be str, not int

    python报错TypeError: must be str, not int 字符串拼接一个整型变量报错,代码如下 for i in range(1,586):res = 'test' + i 报错 ...

  3. python cannot concatenate_解决Python 异常TypeError: cannot concatenate 'str' and 'int' objects

    解决Python 异常TypeError: cannot concatenate 'str' and 'int' objects TypeError: cannot concatenate 'str' ...

  4. python运行报错TypeError: cannot concatenate 'str' and 'int' objects

    dict运行报错 d = {     'Adam': 95,     'Lisa': 85,     'Bart': 59 } for key in d:     print key+":& ...

  5. Python --提示 TypeError: cannot concatenate str and int objects

    clientsocket,addr = socketserver.accept() print("client ip:"+addr[0]) print("client p ...

  6. java indexof int,int indexOf(String str, int fromIndex)

    int indexOf(String str, int fromIndex) 描述 (Description) java.lang.String.indexOf(String str, int fro ...

  7. 成功解决TypeError: object of type ‘int‘ has no len()

    成功解决TypeError: object of type 'int' has no len() 目录 解决问题 解决思路 解决方法 解决问题 TypeError: object of type 'i ...

  8. 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', ...

  9. C语言九十四之请编写函数fun(char *str, int n),其功能是:使字符串str的前导*号不能多余n个,若多于n个,则删除多余的*号,若少于或等于n个,则不做处理。

    1.题目 规定输入的字符串中只包含字母和*号,请编写函数fun(char *str, int n),其功能是:使字符串str的前导*号不能多余n个,若多于n个,则删除多余的*号,若少于或等于n个,则不 ...

最新文章

  1. 作业二:编写一个自动生成四则运算的程序
  2. 使用telnet模拟邮件的收发
  3. 【转载】SAP内部订单概念信息
  4. 计算机作文1500字,作文1500字
  5. php远程下载头像,远程使用gravatar头像
  6. 保护 .NET Core 项目的敏感信息
  7. 融创上海:从营销切入,建立自上而下的数据平台,实现数据应用
  8. Keras自定义Loss函数
  9. 靶形数独(信息学奥赛一本通-T1447)
  10. 【https】keystore was tampered with or password was incorrect
  11. 苹果发布 iOS 和 macOS 更新,修复已遭利用0day
  12. 10个必知的网页设计术语计算机与网络,入学测试计算机与网络应用基础知识题库(公开题)教学文稿(10页)-原创力文档...
  13. inline在C99以及Gcc中的处理方式[转]---很好的一篇总结
  14. win10无法装载iso文件_装载Win10 ISO镜像文件的具体方法
  15. 【事件相机整理】信号处理、噪声与滤波
  16. 2022年全球市场激光直接成像系统(LDI)总体规模、主要生产商、主要地区、产品和应用细分研究报告
  17. k8s健康检查(七)
  18. Android 折叠屏适配最全的攻略在这里
  19. 织梦dedecms 忘记管理员后台密码的解决技巧
  20. 几分钟内上线一个网站,这些神器我爱了!

热门文章

  1. x264编码指南——码率控制
  2. node代理请求 vue_vue-cli项目开发/生产环境代理实现跨域请求+webpack配置开发/生产环境的接口地址...
  3. php可以在dw中吗,php新手求助,为什么在DW中设计里能看到运行php程序的结果,但…...
  4. 语言 泰克示波器程序_示波器再升级,EMI测试不求人
  5. java.sql.SQLException: null,message server: Host ora-rac2 is blocked because of many
  6. liunx安装和部署nacos
  7. layer右下脚弹窗
  8. HDOJ水题集合8:DBFS
  9. 【CCCC】L2-020 功夫传人 (25分),,模拟水题,多叉树的存储与遍历
  10. 计算机博士复试英语自我介绍,博士生复试自我介绍的英文