在Python中显示整数

1 投票
4 回答
16580 浏览
提问于 2025-04-17 04:16

如何在Python中显示整数?

比如说:

number1 = raw_input('Write one number:  ')
number2 = raw_input('Write other number: ')
result =  number1 + number2
print "The sum of the numbers is: %d" % result # here, display the result of the operatio n.

4 个回答

0

像这样;

In [1]: numstr="5"

In [2]: int(numstr)
Out[2]: 5

所以在你的代码中,result = int(number1) + int(number2)

不过你最好加一些输入检查。因为用户常常会犯错误。

1

raw_input 返回的是字符串。如果你想要的是整数,那就用 int(x) 来把它们转换成整数。

3

你想要

result = int(number1) + int(number2)

raw_input 返回的是字符串。

撰写回答