相关疑难解决方法(0)

在Python中将整数转换为字符串?

我想在Python中将整数转换为字符串.我是徒劳地捣蛋:

d = 15
d.str()
Run Code Online (Sandbox Code Playgroud)

当我尝试将其转换为字符串时,它显示的错误就像int没有调用任何属性一样str.

python string integer

1282
推荐指数
11
解决办法
323万
查看次数

如何连接str和int对象?

如果我尝试执行以下操作:

things = 5
print("You have " + things + " things.")
Run Code Online (Sandbox Code Playgroud)

我在Python 3.x中收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int
Run Code Online (Sandbox Code Playgroud)

......以及Python 2.x中的类似错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

python string concatenation python-2.x python-3.x

60
推荐指数
2
解决办法
8万
查看次数

"无法隐式地将'float'对象转换为str"

>>> def main():
        fahrenheit = eval(input("Enter the value for F: "))
        celsius = fahrenheit - 32 * 5/9
        print("The value from Fahrenheit to Celsius is " + celsius)
>>> main()
Enter the value for F: 32
Traceback (most recent call last):  
  File "<pyshell#73>", line 1, in <module>
    main()
  File "<pyshell#72>", line 4, in main
    print("The value from Fahrenheit to Celsius is " + celsius)
TypeError: Can't convert 'float' object to str implicitly"
Run Code Online (Sandbox Code Playgroud)

python typeerror

10
推荐指数
1
解决办法
3万
查看次数

TypeError:无法将'int'对象转换为str隐式错误python

我读了其他的问题,但我试图做的事情是不同的我试图在python中制作一个计算器,并试图将变量输入事物变成一个整数,所以我可以添加它.这是我的代码还没有完成,我是一个初学者:

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)
Run Code Online (Sandbox Code Playgroud)

它能做什么:

Hello! Whats your name?
Jason
What do you want me to do? Jason
I …
Run Code Online (Sandbox Code Playgroud)

int python-3.x

5
推荐指数
0
解决办法
2万
查看次数