python:字符串和数字拼接 (TypeError: Can't convert 'int' object to str implicitly)
来源:互联网 发布:澳洲好还是加拿大 知乎 编辑:程序博客网 时间:2024/05/18 22:10
我将在学习Python的路上,一路狂奔……
之前对java比较熟悉,现在开始学习python,在写代码的习惯上很容易混淆两者,今天就需要到这样的问题,代码如下:
#!/usr/bin/python# -*- coding: UTF-8 -*-from pip._vendor.distlib.compat import raw_inputflag = Falsecount = 1while flag==False and count < 4: guess = raw_input('输入你猜想的数字 ?') guess = int(guess) num = 8 if guess > num : print ('嘿,猜大了,猜了'+count+'次') count = count+1 elif guess == num : print ('嘿,猜对了,猜了'+count+'次') break else : print ('嘿,猜想小了,猜了'+count+'次') count = count+1print ('游戏结束')
print输出了字符串和数字的拼接内容,在java的语法中这样事被允许的,写习惯了,也相当然的以为python这样也可以,谁知道这样事不可以的。在执行的时候报了如下的错误:
输入你猜想的数字 ?9Traceback (most recent call last): File "D:\Workspace_myeclipse\ZwfPython\src\Test1\__init__.py", line 11, in <module> print ('嘿,猜大了,猜了'+count+'次')TypeError: Can't convert 'int' object to str implicitly
看最后一句,不能将int转化成 str,一下子让我好尴尬啊,将 print 代码做了如下的调整:
print ('嘿,猜想小了,猜了'+str(count)+'次')
这样就可以顺利的执行通过了,这件事让我想到了,在不同的编程语言之间,我们虽然总是在不停地强调是相通的,但是每种语言都有自己独树一帜的特性。我们要慎重的看待这些特性,这也说明就是这些应运而生的特性方可以证明编程语言的进步。
1 0
- python:字符串和数字拼接 (TypeError: Can't convert 'int' object to str implicitly)
- selenium--unittest字符串/整型问题Can't convert 'int' object to str implicitly提示解决方法
- Python字符串与数字拼接 TypeError: cannot concatenate 'str' and 'int' objects
- Python字符串与数字拼接报错解决TypeError: cannot concatenate 'str' and 'int' objects
- str convert to int
- Python字符串和数字拼接
- Python:TypeError: unorderable types: int() < str()
- python 3.5: TypeError: a bytes-like object is required, not 'str' when writing to a file
- 字符串转换为数字:String:convert a string to int
- python TypeError: 'int' object is not iterable
- Python:TypeError: 'int' object is not callable
- Python: TypeError: 'int' object is not callable
- python中Base64编码和解码:TypeError: a bytes-like object is required, not 'str'
- UnsupportedOperationException: Can't convert to color (?attr/colorPrimaryn )
- Python---TypeError: '<' not supported between instances of 'str' and 'int'
- TypeError: can only concatenate tuple (not "str") to tuple(python 元组只有一个元素需要加逗号)
- python错误:TypeError: 'str' object is not callable
- Python报错TypeError: 'str' object is not callable
- python 多线程中的守护线程与join的用法
- java程序运行时jvm内存分配
- poj2891 Strange Way to Express Integers (中国剩余定理+拓展欧几里得)
- 2017SyScan360国际前瞻信息安全会议召开通知!
- html5的修炼之路
- python:字符串和数字拼接 (TypeError: Can't convert 'int' object to str implicitly)
- jQuery的append()函数
- .gitignore无效,不能过滤某些文件
- MyEclipse快捷键(个人使用整理)
- jQuery中的事件与动画
- js获取日期以及日期格式化
- PHP怎么打开或者关闭文件?
- 色彩空间
- 15. 3Sum