相关疑难解决方法(0)

如何将输入读作数字?

在下面的代码中为什么xy字符串而不是整数?网上的所有内容都说要使用raw_input(),但是我读input()raw_input()在Python 3.x中重命名的Stack Overflow(在一个不处理整数输入的线程上).

play = True

while play:

    x = input("Enter a number: ")
    y = input("Enter a number: ")

    print(x + y)
    print(x - y)
    print(x * y)
    print(x / y)
    print(x % y)

    if input("Play again? ") == "no":
        play = False
Run Code Online (Sandbox Code Playgroud)

python int input python-2.7 python-3.x

261
推荐指数
7
解决办法
76万
查看次数

如何连接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万
查看次数