Python 为什么我的程序将int添加为字符串(4&x2B;7=47)?

Python 为什么我的程序将int添加为字符串(4&x2B;7=47)?,python,python-2.7,Python,Python 2.7,我不知道为什么它会一直这样添加,我尝试了int()之类的东西,但不起作用。 因此,每当我输入我的数字并吐出结果时,它就会以一种奇怪的方式将其相加,比如10+10=1010,计算机从左到右逐位计算该表达式,如下所示: import time print "1.Addition" print "2.Subtraction" print "3.Multiplication" print "4.Division\n" numChoice = int(raw_input("Type the numb

我不知道为什么它会一直这样添加,我尝试了
int()
之类的东西,但不起作用。
因此,每当我输入我的数字并吐出结果时,它就会以一种奇怪的方式将其相加,比如10+10=1010,计算机从左到右逐位计算该表达式,如下所示:

import time


print "1.Addition"
print "2.Subtraction"
print "3.Multiplication"
print "4.Division\n"

numChoice = int(raw_input("Type the number corresponding to the subject of arithmetic you would like to work with: "))

print "Loading..."

time.sleep(2)
if numChoice==1:
    print "\nYou have chosen Addition!"
    num1Add = int(raw_input("Please enter your first number:"))
    num2Add = int(raw_input("Please enter your second number:"))
    numAddRes = num1Add+num2Add
    print "Calculating..."
    time.sleep(2)
    print num1Add+" plus "+num2Add+" is equal to: "+num2Add+num1Add
请注意,在最后一步中,它会将7添加到字符串中。因此,请确保首先进行整数加法(在该部分周围添加括号):

或者,也可以使用
numadres
变量:

print num1Add+" plus "+num2Add+" is equal to: "+(num2Add+num1Add)

编辑:我测试了这个以确保我没有疯(我实际上不是python用户,但幸运的是,我的机器有python):

>num1=int(原始输入(“输入数字”))
输入一个数字:1
>>>类型(num1)
>>>num2=int(原始输入(“输入数字”))
输入一个数字:4
>>>类型(num2)
>>>res=num1+num2
>>>类型(res)
>>>res
5.
你可以看到,这是预期的工作


EDIT2:好的,python需要显式类型转换来连接int和字符串。所以我不确定,但也许你还没有给出完整的代码?我希望您会看到类似的类型错误

>>> num1 = int(raw_input("Enter a number: "))
Enter a number: 1
>>> type(num1)
<type 'int'>
>>> num2 = int(raw_input("Enter a number: "))
Enter a number: 4
>>> type(num2)
<type 'int'>
>>> res = num1 + num2
>>> type(res)
<type 'int'>
>>> res
5
>“结果:”+num1+num2
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:无法连接'str'和'int'对象
>>>“结果:”+(num1+num2)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:无法连接'str'和'int'对象
>>>“结果:”+str(num1+num2)
‘结果:5’

我试过你说的话,但结果还是一样,就像这样。。。。。。。。。。。。。。。print:num1Add+“加上”+num2Add+”等于“+numadre如果您实际运行的代码有那些
int
调用,您将得到
TypeError
,因为您正在尝试添加int和字符串。你存钱了吗?
print num1Add+" plus "+num2Add+" is equal to: "+numAddRes
>>> num1 = int(raw_input("Enter a number: "))
Enter a number: 1
>>> type(num1)
<type 'int'>
>>> num2 = int(raw_input("Enter a number: "))
Enter a number: 4
>>> type(num2)
<type 'int'>
>>> res = num1 + num2
>>> type(res)
<type 'int'>
>>> res
5
>>> "result: " + num1 + num2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> "result: " + (num1 + num2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> "result: " + str(num1 + num2)
'result: 5'