您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python-3.x – TypeError:无法将’int’对象转换为str隐式错误python

5b51 2022/1/14 8:20:24 python 字数 2124 阅读 450 来源 www.jb51.cc/python

参见英文答案 > TypeError: Can’t convert ‘int’ object to str implicitly????????????????????????????????????2个 我读了其他问题,但我想做的事情是不同的 我试图在python中制作一个计算器,并尝试将变量输入事物变成一个整数,这样我就可以添加它.这是我的代码还没有完成,我是一个初学者: print("Hel

概述

print("Hello! Whats your name?")
myName = input()
print("What do you want me to do? " + myName)
print("I can add,subtract,multiply and divide.")
option = input('I want you to ')
if option == 'add':
    print('Enter a number.')
    firstNumber = input()
    firstNumber = int(firstNumber)

    print('Enter another number.')
    secondNumber = input()
    secondNumber = int(secondNumber)

    answer = firstNumber + secondNumber

    print('The answer is ' + answer)

它能做什么:

Hello! Whats your name?
Jason
What do you want me to do? Jason
I can add,multiply and divide.
I want you to add
Enter a number.
1
Enter another number.
1
Traceback (most recent call last):
File "C:/python33/calculator.py",line 17,in <module>
print('The answer is ' + answer)
TypeError: Can't convert 'int' object to str implicitly

任何帮助,将不胜感激 :)

>>> 'str' + 2
Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
TypeError: Can't convert 'int' object to str implicitly

将int对象显式转换为str对象,然后连接:

>>> 'str' + str(2)
'str2'

或者使用str.format方法

>>> 'The answer is {}'.format(3)
'The answer is 3'

总结

以上是编程之家为你收集整理的python-3.x – TypeError:无法将’int’对象转换为str隐式错误python全部内容,希望文章能够帮你解决python-3.x – TypeError:无法将’int’对象转换为str隐式错误python所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶