TypeError: Can only concatenate str (not “int”) to str (simple Python programme)

后端 未结 11 654
逝去的感伤
逝去的感伤 2020-12-07 05:17

I wrote this simple code and tried to execute in Windows 10 CMD ... and it gets the error message :

TypeError: Can only concatenate str (not \"int\") to str
         


        
11条回答
  •  星月不相逢
    2020-12-07 06:05

    TypeError: Can only concatenate str (not “int”) to str

    This Error states that it can only concatenate string to string. In your program you have tried to concatenate a sting and integer

    Input command would only fetch string from the user, and the age the user enters should be taken as string. Examples: '45', '25', 36 etc..

    This program is trying to concatenate '45' + 2 . Which throws this error.

    Instead you can try converting the user input to int and then concatenate.

    userName = input('Name: ')
    age = int(input('age: '))
    finalAge = age + factor
    factor = 2
    finalAge = age + factor
    print('In ', factor, 'years you will be', finalAge, 'years old', userName, '!')
    

提交回复
热议问题